Skip to content

pcall

Builtin · error handling

Protected call. CL++ has no try/catch; this is how you catch report / Luau error.

auto [ok, result] = pcall(fn);
NameTypeDescription
fnfuncCallback to run. Usually func (…) { }.

Multiple values: success flag, then the result or the error message.

pcall(fn)

Pair with destructuring. If ok is false, result is the error string. There is also Luau xpcall if you include it as a global — the compiler treats it as a call.

For a single method that must not stop the script, : is the short form: workspace:FindFirstChild("x") emits pcall and yields nil on error.

auto [success, result] = pcall(func () {
return DataStore.GetAsync("PlayerData");
});

Emits:

local success, result = pcall(function()
return DataStore:GetAsync("PlayerData")
end)

report · operator-table · destructure · lambda