Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Detecting if a script errors?

Asked by
RM0d 305 Moderation Voter
9 years ago

Is it possible if when a script errors it triggers a Bindable Event

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

Yes, by wrapping your code in pcall:

function runCode()
    --your code here
end

if pcall(runCode) then
    --No error!
else
    --It errored...
end
0
okay thanks :D RM0d 305 — 9y
2
You mean `pcall(runCode)`? Also, I think in general `ypcall` should be preferred BlueTaslem 18071 — 9y
0
Oh that's useful. It's like a Lua try catch. Is there anyway to use the exception as an argument? In Java try{SOME_CODE;}catch(SOME ERROR){SOME_OTHER_CODE;} DewnOracle 115 — 9y
0
@DewnOracle: The second return value of `pcall` is the error message (as a string). You can use `xpcall` to get a bit more fine-tuned error handling by passing a function to run (in the erroring scope) BlueTaslem 18071 — 9y
0
That typo is the reason I prefer anonymous functions to doing this. Yeah, ypcall is probably better if the function yields. adark 5487 — 9y
Ad

Answer this question