bad argument #3 to 'format' (string expected, got boolean)?

3k views Asked by At

Editing a lua file for the rainmeter skin "Do you need a jacket" getting the error in the title for this code


--[[ Given the current temperature, return the appropriate
  string for the main string meter ]]
local function getMainString( temp )
local negation = (temp > Settings.Ss_Limit) and " don't" or ""
local summerwear = (temp < Settings.Ss_Limit) and (temp > Settings.Vest_Limit) and "shirt and shorts"
local innerwear = (temp < Settings.Vest_Limit) and (temp > Settings.Jacket_Limit) and "vest"
local southerwear = (temp < Settings.Jacket_Limit) and (temp > Settings.Coat_Limit) and "jacket"
local outerwear = (temp < Settings.Coat_Limit) and "coat"
return string.format("You%s need a %s", negation, (summerwear or innerwear or southerwear or outerwear))
end

It is supposed to give the correct clothing based on temperature. I have tried with different locations for temperature variation, and the only time i get the error is when the temperature is over Ss_limit. I dont have much coding experience so thank you in advance

2

There are 2 answers

0
fefe On

When temp is larger than Settings.Ss_Limit, or equals any of the Settings.*_Limit, all summerwear, innerwear, southerwearand coatwear will be false. This make (summerwear or innerwear or southerwear or outerwear) to be false (a boolean) instead of a string which is causing the error.

Possible fix:

--[[ Given the current temperature, return the appropriate
  string for the main string meter ]]
local function getMainString( temp )
local negation = (temp > Settings.Ss_Limit) and " don't" or ""
--[[ this is used to produce "You don't need a cloth" when
    temp is greater than Ss_Limit. Adjust the string based on your own need.
]]
local clothwear = (temp > Settings.Ss_Limit) and "cloth"
--[[ changed < to <=, the following is the same, as not to get an error
  when temp equals any of the _Limit .
]]
local summerwear = (temp <= Settings.Ss_Limit) and (temp > Settings.Vest_Limit) and "shirt and shorts"
local innerwear = (temp <= Settings.Vest_Limit) and (temp > Settings.Jacket_Limit) and "vest"
local southerwear = (temp <= Settings.Jacket_Limit) and (temp > Settings.Coat_Limit) and "jacket"
local outerwear = (temp <= Settings.Coat_Limit) and "coat"
--[[ added clothwear here, to produce proper output 
  when temp is greater than Ss_Limit
]]
return string.format("You%s need a %s", negation, (clothwear or summerwear or innerwear or southerwear or outerwear))
end
0
findux On

You need to manually evaluate boolean type to string.

Try this,

string.format("You%s need a %s", negation, tostring(clothwear or summerwear or innerwear or southerwear or outerwear))