Skip To Content
Not affiliated with Roblox Corporation. 9Roblox never asks for your password and never offers free Robux.

Luau If Statements Explained With Roblox Examples

Roblox Studio · Scripting Basics · Updated

Master Luau if, elseif, and else blocks, relational and logical operators, guard clauses, and inline if expressions with Roblox Studio examples.

Difficulty
Beginner
Time Needed
15 Minutes
Core Keywords
if, then, elseif, else, end
Not Equal Operator
~= (Not !=)
Logical Operators
and, or, not
Inline Expression Rule
if cond then valA else valB (No end; else Required)

Structure Of If Elseif And Else Blocks

Conditional statements let a Roblox script make decisions at runtime—checking whether a player owns enough coins, whether a door is locked, or whether a character still has health left. Every standard block starts with if, follows its condition with then, and closes with end. Optional elseif branches test backup conditions in order, and an optional else branch runs only when every condition above it fails.

Luau evaluates an if-elseif-else chain strictly from top to bottom and stops the moment it hits the first truthy condition. Because later branches are skipped once a match wins, numerical ladders should order thresholds carefully from highest to lowest (or lowest to highest): checking level >= 10 before level >= 20 would trap level 25 players inside the lower tier forever. Note also that elseif is a single word with no space; typing else if opens a second nested block that requires an extra end.

Compare Values With Relational And Logical Operators

Most conditions compare numbers, strings, or properties using six relational operators that return either true or false. Developers switching from JavaScript, Python, or C# often stumble on inequality: Luau uses ~= for not equal rather than !=, and uses double equals (==) for comparisons while reserving single equals (=) for variable assignment.

To combine multiple checks inside one condition, use the logical operators and, or, and not. Both and and or short-circuit: in a and b, Luau skips evaluating b whenever a is false or nil. That behavior makes safe instance checks straightforward—writing character and character:FindFirstChild("Humanoid") prevents a runtime error when character happens to be nil. Wrap mixed and/or expressions in parentheses so operator grouping stays unmistakable.

Write Shorter Conditionals With Inline If Expressions

Modern Luau adds inline if-then-else expressions that pick a value directly inside a variable assignment or function call, replacing clunky multi-line blocks. Unlike standard if statements, an inline if expression does not finish with end, can include elseif clauses, and always requires a closing else branch.

Inline expressions also solve a well-known classic Lua bug. Older tutorials often fake ternary logic with condition and valueA or valueB, which breaks silently whenever valueA happens to be false or nil by returning valueB instead. Writing if condition then valueA else valueB handles false and nil cleanly in every case.

Keep Roblox Event Handlers Flat Using Guard Clauses

Gameplay events such as Touched or PlayerAdded fire constantly—including when stray accessories, tools, or non-player parts brush against a trigger. Wrapping every validation inside nested if blocks quickly pushes logic six indentation levels to the right and makes scripts hard to debug.

Instead, exit invalid cases immediately at the top of the function using guard clauses such as if not humanoid then return end. Once every guard clause passes, the remaining code runs flat, readable, and guaranteed to hold valid references.

Build A Tiered Speed Pad In Luau

Create an anchored Part named BoostPad inside Workspace, then place the Script below into ServerScriptService. The script combines guard clauses, a per-player cooldown check using os.clock(), an ordered if-elseif-else tier check based on a custom Level attribute, and an inline if-then-else expression to label the boost tier in Output.

local Players = game:GetService("Players")
local pad = workspace:WaitForChild("BoostPad")

local cooldowns: { [number]: number } = {}
local COOLDOWN_SECONDS = 3

pad.Touched:Connect(function(hit)
	local character = hit.Parent
	if not character then
		return
	end

	local humanoid = character:FindFirstChildOfClass("Humanoid")
	local player = Players:GetPlayerFromCharacter(character)
	if not humanoid or not player then
		return
	end

	local now = os.clock()
	if cooldowns[player.UserId] and (now - cooldowns[player.UserId]) < COOLDOWN_SECONDS then
		return
	end
	cooldowns[player.UserId] = now

	local level = player:GetAttribute("Level") or 1
	local boostSpeed: number
	if level >= 20 then
		boostSpeed = 32
	elseif level >= 10 then
		boostSpeed = 24
	else
		boostSpeed = 20
	end

	local tierLabel = if level >= 20 then "Master" elseif level >= 10 then "Scout" else "Rookie"
	humanoid.WalkSpeed = boostSpeed
	print(player.Name .. " (" .. tierLabel .. ") received WalkSpeed " .. tostring(boostSpeed))
end)

Fix Common Conditional Bugs In Roblox Scripts

When an if statement never runs—or always runs—three mistakes cover almost every beginner bug. First, remember that 0 and empty strings evaluate as true in Luau; write if coins > 0 then instead of if coins then when testing whether a player has currency left.

Second, comparing mismatched types such as the string "10" against the number 10 with == always yields false without raising an error, so convert text box inputs with tonumber() before comparing. Third, watch for missing ~= syntax or an accidental single = inside a condition, both of which the Studio Script Editor underlines in red before playtesting.

More In Scripting Basics