Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you make pcall yield properly?

Asked by 4 years ago

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

Log in to vote
0
Answered by
cfiredog 274 Moderation Voter
4 years ago

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.

0
Very annoying method, I guess it'll have to do. Thanks! Smash108 70 — 4y
Ad
Log in to vote
0
Answered by
kisty1 111
4 years ago
Edited 4 years ago

You can't yield in a pcall. Use a ypcall instead of a pcall.

Answer this question