Tutorials/Types/Booleans

From FiguraMC
Revision as of 04:29, 27 September 2024 by PoolloverNathan (talk | contribs) (why do y'all write so big)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A boolean is a Lua value that is either true or false. Additionally, Lua considers any value except nil and false a truthy value. A truthy value in the condition part (if CONDITION then) part of a condition will act like true and run the code in the then section.

Example:

if 0 then
    -- numbers are truthy, so this will always be printed
    print("truthy")
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. This will skip the code after if, and any else code will be run, if present.

Example:

if nil then
    -- this will never be reached
    print("truthy")
else
    -- nil is falsey, so this will always be printed
    print("falsey")
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. For example, `not true`, `not 0`, and `not hello` are all false, while `not false` and `not nil` are true.

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.