If a request is made to an API via HttpService in a script, and the API returns a string value, does Roblox assume it’s true?
For example, if I have a string that says if Callback.Success then which checks if an API returns true, and a string is sent from the API to Roblox, will Roblox assume it is true?
Also, does using if <var> true then and if <var> == true then have a difference in my code?
if <var> == true obviously checks if it’s true, while if <var> then checks if it’s true, or existent. Finally, if it’s false or nil, it will return false.
if <var> then obviously isn’t really much of an efficient way to check if something is nil or not. If you wanted to check if it was nil and not false, you could do if <var> == nil or for confirmation of the instance’s existence, if <var> ~= nil>.
No, Roblox will not assume that if Callback.Success then is true, rather, confirm its existence and that it’s not either false or nil, and if not, the if statement wil proceed to do its job.
So if a string is returned, and there is an if statement checking for if it’s true, it will return, correct? And is there a difference in performance in checking if something is true using if var == true then and if var then?
A better term would be boolean evaluation. And correct it’ll evaluate to true.
Anything but false and nil evalutes to true (in Lua).
I might be wrong but as it’s Luau (and there’s a lot optimisations that’s happend in Luau), I don’t think there’s a (noticable) difference in performance for if var == true then and if var then.
Lua considers values that aren’t falsey (false or nil) to be truthy, such as strings, numbers, tables and userdatas. If you mean comparing it to true, it will return false. @xxIamInevitable for the correction
if "a" then
print("Executed!") -- Executed!
end
if "a" == true then
print("Executed too!") -- Doesn't execute
end
Once again, Lua (and probably other programming languages) takes the existence of an instance or an object truthy, not falsy, which is why it returns true.