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?
✓ ANSWER ACCEPTED
How to "fail safe" a script in the event something errors?
1 answer
✓ Accepted answer
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
3 comments
1
you actually do not need to use variables.
1
well yes, but it is a little easier to understand in my opinion
0
Yes, this is a lot easier to understand
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
https://developer.roblox.com/articles/Built-in-Functions-and-Variables/Lua#pcall
https://www.lua.org/pil/8.4.html