Syntax
local label = if count > 0 then "Available" else "Empty"Examples
Test the actual requirement
Standalone Luau or a Script in ServerScriptService. A count of zero takes the empty branch even though zero itself is truthy.
local count = 0
if count > 0 then
print("Available")
elseif count == 0 then
print("Empty")
else
print("Invalid count")
endKeep a false result
An if expression can return false without falling through to a fallback. The older and/or idiom below cannot represent that choice reliably.
local enabled = true
local value = if enabled then false else "disabled"
local misleading = enabled and false or "disabled"
print(value) -- false
print(misleading) -- disabledBest practices
- Use == for equality and ~= for inequality; = assigns a value.
- Put parentheses around mixed arithmetic and boolean expressions when the intended grouping is not obvious.
- Use and/or short-circuiting deliberately; the result can be a value rather than a boolean.
At a glance
- Purpose
- Typed scripting and Roblox development
- File extension
- .luau
- Runs in
- Luau host; Roblox engine examples require Roblox Studio
- Usually used with
- Roblox APIs and Studio
Specifications & further reading
Related Luau documentation
Variables, values and scope
Use local names for values that belong to a function or block. A nested block can read outer locals, but a new local with the same name shadows the outer binding. nil represents absence; false is an actual boolean value.Loops and iteration
Choose a numeric for loop for a known range, a table iterator for a collection, or a while loop for a condition. break leaves the loop and continue skips the rest of one iteration. Repeated engine work needs a schedule, not an unbounded busy loop.Inference, annotations and strict mode
Luau can infer types from expressions, while annotations document the boundary of a function or record. Strict mode reports more incomplete assumptions. Static checks help authors; runtime checks still protect values received from players or saved storage.Luau and Roblox scripting
Start with Luau values and functions, then use Roblox Studio to connect that code to players, Instances and events. Luau is the language; Roblox supplies the game engine APIs. Codectionary supports editing and highlighting, while these Roblox examples run in Studio.
Use local names for values that belong to a function or block. A nested block can read outer locals, but a new local with the same name shadows the outer binding. nil represents absence; false is an actual boolean value.Loops and iteration
Choose a numeric for loop for a known range, a table iterator for a collection, or a while loop for a condition. break leaves the loop and continue skips the rest of one iteration. Repeated engine work needs a schedule, not an unbounded busy loop.Inference, annotations and strict mode
Luau can infer types from expressions, while annotations document the boundary of a function or record. Strict mode reports more incomplete assumptions. Static checks help authors; runtime checks still protect values received from players or saved storage.Luau and Roblox scripting
Start with Luau values and functions, then use Roblox Studio to connect that code to players, Instances and events. Luau is the language; Roblox supplies the game engine APIs. Codectionary supports editing and highlighting, while these Roblox examples run in Studio.