Is there a function that can run when a Script errors?
1 answer
Simply create a function where you can pass another function as an argument, checking to see if anything has gone wrong within that passed function using pcall. Here's an example:
local function GoneWrong(f)
if not pcall(f) then -- If an error in the function was detected, then...
print("The function could not complete")
end
end
Let's test it by trying to call a nil value:
GoneWrong(function() -- Passing an anonymous function
null() -- Trying to call something that doesn't exist
end)
Our output window should display our error message:
The function could not complete
Hope this helped, let me know if you have any questions.
2 comments
This is unnecessary abstraction. pcall itself answers the question.
Didn't he want to know how to create a function that would execute explicitly when code errors? Pcall is still the primary logic around it, but there's still more you have to write, and depending on your code, different ways you can write it.
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
If you want something to prevent a script from crashing upon an error, use
pcall. It will just ignore the function and continue instead of stopping the entire script. Otherwise, you may need to be more specific in the question.http://wiki.roblox.com/index.php?title=Function_dump/Basic_functions#pcall