match
Type switch. Instance types use IsA; primitives use typeof. _ is the fallback.
Syntax
Section titled “Syntax”match (value) { Type name => statement, Other o => statement, _ => statement};Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
value | any | Scrutinee. Evaluated once. |
Return value
Section titled “Return value”None (statement).
Luau emit
Section titled “Luau emit”if x:IsA("Type") then … elseif typeof(x) == "…" then …
Description
Section titled “Description”Arms are a class or primitive name plus a binding (Part p, string s) — not a C++ pointer. Instance arms emit IsA("Part"). _ is required if the match is not exhaustive in practice — always provide it.
This is not C++ std::variant visit and not Luau if-then-else expressions.
Example
Section titled “Example”match (instance) { Part p => p.BrickColor = BrickColor::Red(), Model m => m.PrimaryPart.BrickColor = BrickColor::Blue(), _ => warn("Instance not supported")};Emits:
if instance:IsA("Part") then local p = instance p.BrickColor = BrickColor.Red()elseif instance:IsA("Model") then local m = instance m.PrimaryPart.BrickColor = BrickColor.Blue()else warn("Instance not supported")end