PCall - Confused On Behavior

Why is the pcall not catching both the errors in the sample code below?
It seems to catch the error, but then also reports a stack dump?

local success, message = pcall(function()

	-- Error 1
	wait(UNDEFINED_VARIABLE)
	
	-- Error 2
	if not UnDefinedPlayerData[player] then
		-- Test
	end
	
end)
if not success then
	print("My Pcall error: ", message)
end

Output:
[My Pcall error: ServerScriptService.Script:7: attempt to index global ‘UnDefinedPlayerData’ (a nil value)]

[14:49:45.160 - ServerScriptService.Script:7: attempt to index global ‘UnDefinedPlayerData’ (a nil value)]

14:49:45.160 - Stack Begin

[14:49:45.160 - Script ‘ServerScriptService.Script’, Line 7]

14:49:45.161 - Stack End

wait(UNDEFINED_VARIABLE) roughly translates to wait(nil), which does not error.

Yielding inside a pcall, however, will always report errors even after they are caught.

3 Likes

So having a wait() inside a pcall will cause it to report the errors?

Does that mean that the error will not be caught at all, and the code will break due to the exception? Or does the reporting just sneak through, and the error is still caught?

Reporting sneaks through but the error is caught. Any sort of yielding within a pcall causes this behavior.

2 Likes

Try ypcall or whatever the alternative on wiki was

1 Like

Also its not supposed to catch all errors, it catches whatever error happens to occur if there is one. You cannot make a single thread or protected call error more than once because it errors and exits.

pcall references to ypcall, so they are both the same internally. Using one or the other won’t make a difference