pcall
Protected call. CL++ has no try/catch; this is how you catch report / Luau error.
Syntax
Section titled “Syntax”auto [ok, result] = pcall(fn);Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
fn | func | Callback to run. Usually func (…) { }. |
Return value
Section titled “Return value”Multiple values: success flag, then the result or the error message.
Luau emit
Section titled “Luau emit”pcall(fn)
Description
Section titled “Description”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.
Example
Section titled “Example”auto [success, result] = pcall(func () { return DataStore.GetAsync("PlayerData");});Emits:
local success, result = pcall(function() return DataStore:GetAsync("PlayerData")end)