Tutorials/Types/Booleans

From FiguraMC
Revision as of 20:45, 26 September 2024 by Manuel (talk | contribs) (Automated upload of converted .txt file.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A boolean is a Lua value that is either true or false.


Additionally, Lua treats all values as if they’re ‘truthy’ or ‘falsey’.


If a value is truthy and it’s in an if statement or a function that wants a boolean value, it’s treated like it’s true.


Example:


if 0 then

    log("truthy") -- numbers are truthy so this will always be logged

else

    log("falsey")

end

If a value is falsey and it’s in an if statement or a function that wants a boolean value, it’s treated like it’s false.


Example:


if nil then

    log("truthy")

else

    log("falsey") -- nil is falsey so this will always be logged

end

The only values that are falsey are false and nil, every other value (numbers, tables, modelparts, etc) is truthy. (nil as a value means there’s no information. It’s literally nothing)


The not operator flips the truthiness of the value into true or false.


Examples (all of these are true statements):


not true == false

not false == true

not nil == true -- a non-boolean value is turned into a boolean

not models == false -- models is a modelpart and truthy, so flipping it turns it into false

When used in methods true usually activates something, and false deactivates it. However in some places returning true may turn something off, always read the description of a method, field, or event to discover what boolean does what.