Decisions, lesson 7
if, elseif, else
Goal: Run different code for different situations.
about 6 min4 practice exercises
An if statement runs code only when a condition is true.
local coins = 12
if coins >= 10 then
print("The gate opens")
else
print("You need more coins")
end
The pattern is always if condition then ... end. The else part is optional and runs when the condition is false.
To check several cases, add elseif. Luau tests them from the top and runs the first one that is true, then skips the rest:
if coins >= 50 then
print("Gold rank")
elseif coins >= 10 then
print("Silver rank")
else
print("Bronze rank")
end
With 12 coins this prints Silver rank. Order matters: if you checked coins >= 10 first, a player with 80 coins would also get Silver, because that test is true for them and it comes first.
Sources
Checked against Roblox's documentation on . The lesson is in our own words; these pages are the source of truth.