Roblox If Statements in Luau: Conditions, Operators and nil Checks
Last updated:
Quick answer: An if statement runs code only when a condition is true: if x then ... elseif y then ... else ... end. Luau compares with ==, ~=, <, >, <= and >=, combines conditions with and, or and not, and treats only nil and false as false, so 0 and empty strings count as true.
An if statement in Luau runs a block of code only when a condition is true. The full form is if condition then ... elseif other then ... else ... end. Conditions are built from comparison operators (==, ~=, <, >, <=, >=) and combined with and, or, and not. Only nil and false count as false.
What is the basic if / elseif / else syntax?
local coins = 25
if coins >= 100 then
print("Rich")
elseif coins >= 20 then
print("Comfortable")
else
print("Broke")
end
-- prints Comfortable
Luau checks the branches from top to bottom and runs only the first one that is true. elseif is one word, there are no parentheses or curly braces around the condition, and every if must end with its own end. The else part is optional.
Which comparison operators does Luau have?
| Operator | Meaning |
|---|---|
== | equal to |
~= | not equal to (not !=) |
<, > | less than, greater than |
<=, >= | less than or equal, greater than or equal |
The two mistakes that trip up almost everyone: using a single = (assignment) where you need ==, and writing != from other languages. Both are syntax errors in Luau. You can compare numbers, strings, and Instances; comparing a number to a string, as in 5 == "5", is simply false, and using < between them throws an error.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
if player.Name == "Builderman" then
print("Hello, builder")
end
if player.AccountAge < 7 then
print(player.Name .. " is a new account")
end
end)
How do and, or and not work?
and requires both sides to be true, or requires at least one, and not flips a value:
local function canEnterVip(player)
local leaderstats = player:FindFirstChild("leaderstats")
local coins = leaderstats and leaderstats:FindFirstChild("Coins")
if coins and coins.Value >= 500 and not player:GetAttribute("Banned") then
return true
end
return false
end
Luau stops evaluating as soon as the result is known (short-circuiting). That is why coins and coins.Value >= 500 is safe: if coins is nil, the second half never runs and nothing errors. This pattern prevents most attempt to index nil errors.
and and or also return one of their operands, which gives you a compact fallback and a ternary-style pick:
local displayName = player.DisplayName or player.Name
local label = isAlive and "Alive" or "Dead"
-- caution: this breaks if the middle value is false or nil
Luau also has a dedicated if-expression that avoids that caveat: local label = if isAlive then "Alive" else "Dead".
What counts as true or false in Luau?
Only two values are falsy: false and nil. Everything else is truthy, including 0, the empty string "", and empty tables.
if 0 then print("0 is truthy") end -- prints
if "" then print("empty string is truthy") end -- prints
if nil then print("never") end -- does not print
If you are coming from JavaScript, Python or C, this is the single biggest surprise. To test for zero or an empty string, compare explicitly: if count == 0 then, if text == "" then.
How do I check for nil in Roblox?
Roblox functions like FindFirstChild, GetPlayerFromCharacter and :GetAttribute return nil when nothing is found, so you check before using the result:
local Players = game:GetService("Players")
local part = workspace:WaitForChild("Door")
part.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if not player then
return -- not a player, stop here
end
local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
if humanoid and humanoid.Health > 0 then
print(player.Name .. " opened the door")
end
end)
Early returns like if not player then return end keep the happy path unindented and are easier to read than five nested if blocks. For the underlying event, see the Touched event guide. To test for nil specifically (and treat false differently), use if value == nil then.
Can I nest if statements, and what about a switch?
You can nest as deeply as you like, but flat is usually clearer. Luau has no switch, so use an elseif chain or a lookup table:
local rewards = { Easy = 10, Medium = 25, Hard = 50 }
local function getReward(difficulty)
return rewards[difficulty] or 0
end
print(getReward("Hard")) -- 50
print(getReward("Impossible")) -- 0
A table lookup replaces a long elseif chain and is easier to extend. See Luau tables, arrays and dictionaries.
Common mistakes with Luau if statements
- Using
=instead of==.if coins = 10 thenis a syntax error. Use==. - Writing
!=. Luau's not-equal is~=. - Forgetting
thenorend. The Output window reports an "expected 'then'" or "expected 'end'" error. See fix expected near syntax error. - Assuming 0 or "" is false. Both are truthy. Compare explicitly.
- Indexing before checking.
hit.Parent.Humanoid.Healtherrors when there is no Humanoid. UseFindFirstChildOfClassand test the result first. - Chaining
orwrongly.if x == 1 or 2 thenis always true because2is truthy. Writeif x == 1 or x == 2 then. - Comparing different types.
"10" < 5throws an error; convert withtonumberfirst.
Where to go next
Conditions are what make loops and functions useful: loop over players and use if to filter them, or return early from a function when input is invalid. Continue with loops in Luau and functions in Luau, or revisit the full Roblox Luau tutorial to see everything working in a mini-game.
Frequently asked questions
Is there a not equal operator in Luau?
Yes, it is ~=. Luau does not use != like JavaScript or Python. For example, if player.Team ~= nil then runs only when the player is on a team. Using != causes a syntax error.
Is 0 true or false in Luau?
It is true. Luau treats only nil and false as false, so 0, the empty string, and empty tables are all truthy. If you want to test for zero, write if amount == 0 then explicitly.
How do I check if something is nil in Roblox?
Use if value then to run code when it exists, if not value then when it is missing, or if value == nil then when you need to tell nil apart from false. Always check results of FindFirstChild, GetPlayerFromCharacter and GetAttribute before indexing into them, because they return nil when nothing is found.
Does Luau have a ternary operator?
Luau has an if-expression: local text = if score > 10 then "High" else "Low". It is safer than the older condition and a or b trick, which fails when a is false or nil. Both forms work, but the if-expression is the clearer choice in new code.
Master arithmetic, relational, and logical operators in Luau to compute expressions and conditions.