Skip to content

Commit fb52a5c

Browse files
authored
Add example usage function for pcall() (#1114)
## Changes The documentation can be somewhat confusing when trying to learn how to correctly implement pcalls as pointed out by @junveld in [this forum post](https://devforum.roblox.com/t/feedback-on-luau-globals/3664364). I have added a small piece of example code for how to use pcalls to do function calls that even when returning an error, will allow the rest of the script to continue as normal. ## Checks By submitting your pull request for review, you agree to the following: - [x] This contribution was created in whole or in part by me, and I have the right to submit it under the terms of this repository's open source licenses. - [x] I understand and agree that this contribution and a record of it are public, maintained indefinitely, and may be redistributed under the terms of this repository's open source licenses. - [x] To the best of my knowledge, all proposed changes are accurate. ---------
1 parent 8ae4558 commit fb52a5c

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

content/en-us/reference/engine/globals/LuaGlobals.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,20 @@ functions:
346346
errors. In such case, `pcall()` also returns all results from the call,
347347
after this first result. In case of any error, `pcall()` returns false
348348
plus the error message.
349+
350+
```lua
351+
local function divideByFive(n: number): number
352+
return n / 5
353+
end
354+
355+
local success, errorMessage = pcall(divideByFive, "notANumber") -- Results in error...
356+
357+
if success then
358+
-- Handle successful response...
359+
else
360+
warn("Error message:", errorMessage)
361+
end
362+
```
349363
parameters:
350364
- name: func
351365
type: function

0 commit comments

Comments
 (0)