Skip to content

match

Control flow

Type switch. Instance types use IsA; primitives use typeof. _ is the fallback.

match (value) {
Type name => statement,
Other o => statement,
_ => statement
};
NameTypeDescription
valueanyScrutinee. Evaluated once.

None (statement).

if x:IsA("Type") then … elseif typeof(x) == "…" then …

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.

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

switch · guard · static_cast · instance-pointer