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

How do you make pcall yield properly?

As simple as this.

Why does this error:

local suc,err = pcall(function()
	wait(1)
	print(game.anerror)
end)

And this doesn't:

local suc,err = pcall(function()
	print(game.anerror)
end)

And is there a way to make neither of them error so I can handle errors properly?

2 answers

✓ Accepted answer

The current answer is incorrect. In standard Lua, you are unable to yield inside of a pcall. However, there are some differences in how this works on Roblox. On Roblox, pcall and ypcall are the same, so both will generate an error that is not suppressed.

The problem stems instead from the implementation.

Roblox uses continuations which are a way of specifying what to do after a thread terminates. If a thread yields, those continuations are passed along each time the thread resumes.

However, continuations do not consider error suppression. So, when a thread such as pcall is resumed, the errors will be displayed in the output after it has yielded once.

For now, you will need to wrap each individual statement inside of a pcall to suppress the errors.

1 comment

0

Very annoying method, I guess it'll have to do. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.