guard
If the condition is false, the else block runs (usually return). Early-out, not an if replacement.
Syntax
Section titled “Syntax”guard (condition) else { warn("…"); return;}Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
condition | bool | Must be true to continue. |
Return value
Section titled “Return value”None. The else block typically returns.
Luau emit
Section titled “Luau emit”if not (condition) then … end
Description
Section titled “Description”The else is required. After a successful guard, the compiler (and the reader) treat the condition as established — e.g. player != null.
Example
Section titled “Example”guard (player != null) else { warn("Invalid player"); return;}post(player.Name);Emits:
if not (player ~= nil) then warn("Invalid player") returnendprint(player.Name)