Codectionary / Developer documentation / Luau

Conditionals and operators

Use if statements to choose actions and if expressions to choose values. Only nil and false are falsey. A condition like if count does not test whether a count is positive, because zero is truthy.

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")
end

Keep 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) -- disabled

Best 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