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

How to "fail safe" a script in the event something errors?

Asked by 5 years ago

As I continue to optimise my scripts, I've heard about a function which deals with errors within a script in the event that an error does occur it will continue. I understand that this is commonly used for data retrieval using at async function but can someone explain this to me?

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

Well the failsave is the built in pcall function which wraps everything inside it in a protected bit of code. If the code inside it errors it will return two thinks, a success value (boolean) and a error message(string) and if it is successful, it runs and returns a success value.

local success, Error = pcall(function()
    pprint("test")--errors
end)

if success then 
    print("yay",Error)--"yay",  false
else
    print(Error)
end
1
you actually do not need to use variables. User#21908 42 — 5y
1
well yes, but it is a little easier to understand in my opinion theking48989987 2147 — 5y
0
Yes, this is a lot easier to understand Marmalados 193 — 5y
Ad

Answer this question