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

coroutine.yield() at the beginning of a script?

So, i know basically what coroutine.yield() does. I know it "pauses" a coroutine function, and picks up where it left off when called again. But I've seen this script that has "coroutine.yield()" on line 1 of their script, literally.

coroutine.yield()

-- Script continues

Just curious, what does this do? Is this valid? What could it be used for? Thank you for taking the time to read this, hope you can help.

1 answer

✓ Accepted answer

What I believe is happening is that ROBLOX's coroutine scheduler is immediately resumeing any Script threads that get yielded. This sort of makes sense, since there's no way for any script you write to resume another script (there's no way to get the active coroutine).

The yield is actually happening -- it's not just being ignored. I can tell because when I run these two scripts

print("a")
coroutine.yield()
print("b")
print("c")
coroutine.yield()
print("d")

I get

a
c
b
d

as the output. This wouldn't be possible if coroutine.yield didn't actually yield the thread.


You can yield with a value. I wonder whether or not this corresponds to a builtin function like wait. I couldn't get it to happen, though.


I can't imagine a reason for why you would do this. It might help with making sure other scripts "run" "before" this one, but that probably isn't very predictable.

It's definitely strange. I don't think it should be put to any use.

1 comment

1

Thanks for the explanation, very much appreciated.

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