Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a function that can run when a Script errors?

1 answer

✓ Accepted 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

0

This is unnecessary abstraction. pcall itself answers the question.

0

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.