< Scripting | Data Types

A Boolean, or Bool for short, is a very simple data type. It is either a true or false value.

Using Booleans

Booleans are most commonly used with conditional statements.

MyBool = true
if MyBool then
    --If "MyBool"'s value is true, this code is run.
    --If "MyBool"'s value is false, this code is run.


In Lua, if a value not is false or nil then it is considered "truthy". When used in an if, while, or repeat statement, truthy values are treated as if they were true, and other values are treated as false. The code below prints out only the values which are truthy:

-- these values are all "truthy"
if true           then print("true") end
if 1              then print("1") end
if "text"         then print('"text"') end
if {1, 2, 3}      then print("{1, 2, 3}") end
if game.Workspace then print("game.Workspace") end
-- So are these, despite what you might expect
if 0  then print("0") end
if "" then print('""') end
if {} then print("{}") end
-- But these values are not
if false then print("false") end
if nil   then print("nil") end
{1, 2, 3}

Converting booleans into strings

Note:When trying to concatenate Boolean values you must use the tostring function or it will throw an error.

When converted to a string, booleans will return true or false


If you want to print something other than true or false, you must use conditional statements.

local enabled = false
if enabled then

You can also use the following idiom to get the same results:

print(enabled and "Enabled" or "Disabled")



The not operator returns true if the argument is false or nil, otherwise it will return false.

"not" Operator Sample Results
Value of x Value of not x
true false
false true
nil true
"text" false
0 false
1 false

One thing that the not operator is useful for is toggling something. To make a button toggle the visibility of a GUI, you can do:

    frame.Visible = not frame.Visible


The and operator returns the first argument if it is false or nil, otherwise it will return the second argument.

print(4 and 5)         --> 5
print(nil and 13)      --> nil
print(false and 13)    --> false
print(true and true) -- true
print(true and false) -- false
print(false and true) -- false
print(false and false) -- false

print(true and nil)
print(false and 5)
print({} and "hello")


The 'or' operator operates on two values. If the first value is neither false nor nil, the 'or' operator returns the first value. If the first value is false or nil, then it will return the second value, regardless of what it is.

The 'or' operator comes in handy when you want to check if one of the listed values is a certain value.
soul = true
food = false
try = false
if soul == true or food == true or try == true then -- If any of the three comparisons are met, then continue
    print("The value of either soul, food or try is true.")
The value of either soul, food or try is true.

Useful idioms

Choice of value

The 'or' operator can also be used to choose an default value if a value is nil or false. Here are some examples:

local y = x or 1

This printed '1' because x doesn't exist and is therefore nil. So the or operator allowed us to choose 1 over nil.

local x = false
local y = x or 1

This also printed '1' because although x exists, it is false. If x had been true, then y would be true, because the 'or' operator would choose x, as it is neither false, neither nil.

Conditional expression

A conditional expression, sometimes referred to as the ternary operator, is a substitute for a conditional statement that can be used as part of a larger expression. It allows this

local x = 100
local msg
if x > 50 then
    msg = "is large"
    msg = "is small"
msg = "your value " .. msg

To be transformed into this functionally equivalent code:

local x = 100
local msg = "your value " .. (x > 50 and "x is large" or "x is small")

It is important to note that for the general case of cond and x or y, the code will not behave as expected if `x` is not truthy.

For more in-depth discussion of this construct, see TernaryOperator on

See also




If two people both likes cats, then they can be friends. We want to see if Joe and Bill can be friends based on the variables JoeLikesCats and BillLikesCats.

Here is some code to test your output:

JoeLikesCats= false
BillLikesCats = true

--should output "false" with the above code
print(JoeLikesCats and BillLikesCats)



Jerry wants to go to the beach, but he isn’t sure if he has everything he needs. Jerry cannot go if he does not have sunblock, either a blueSwimsuit or a purpleSwimsuit, a greenTowel, redTowel, or orangeTowel. Furthermore it must not be raining or coldOutside for Jerry to be able to go to the beach. Using what you learned in this tutorial, write a script that outputs true if Jerry can go to the beach and false if Jerry cannot go. Use the bolded variables in your solution.

Here's some code to test your output:

sunblock = true
blueSwimsuit = true
purpleSwimsuit = false
greenTowel = false
redTowel = true
orangeTowel = false
raining = false
coldOutside = false

--should output "true" with the above code
print(sunblock and (blueSwimsuit or purpleSwimsuit) and (greenTowel or redTowel or orangeTowel) and not raining and not coldOutside)