Decisions, lesson 9
nil and truthiness
Goal: Handle a value that is not there.
about 5 min4 practice exercises
nil means "no value". A variable you never set is nil. So is something you look up that does not exist.
local pet
print(pet) -- nil
In a condition, Luau treats exactly two values as false: false and nil. Everything else counts as true, including 0 and the empty string "".
local coins = 0
if coins then
print("This prints, because 0 is not false or nil")
end
If you mean "has more than zero coins", say so: if coins > 0 then.
This rule has one very useful side. To check that something exists before you use it, just test it:
if pet then
print("You have a pet")
else
print("No pet yet")
end
You will use this constantly in Roblox, where "the thing I was looking for is not there" is an everyday event.
Sources
Checked against Roblox's documentation on . The lesson is in our own words; these pages are the source of truth.