As a Roblox developer, it is currently too hard to yield a coroutine from outside the coroutine.
I have a script that when you click an object, it will try to yield 5 scripts, but cannot do so as it uses a bindable function. But a yield can only work if inside the coroutine, and therefore This does not work.
If Roblox is able to address this issue, it would improve my development experience because it would be easier to pause things on user input with coroutines.
P.S. I think the best way to solve this is to use something called coroutine.Pause(). coroutine.Pause() would work similarly to coroutine.Yield() but would force you to define what coroutine you want to suspend inside the parenthesis (otherwise returning an error).
How would this work, though? Coroutines don’t actually run in parallel, so you’d be yielding an already “yielded” coroutine. Would it just stop say, wait(2) from continuing if the coroutine is yielded from elsewhere?
Also what happens to code that is in the middle of crucial execution? If you yield a coroutine that’s waiting to return from a yield function (like :WaitForChild()) you end up with incomplete logic where an action is partially completed but it’s not stopped at a consistent spot.
There’s almost certainly a better way of achieving what you want to do.
Yielding a coroutine from outside of it fundamentally isn’t possible because there is only one non-yielded coroutine at a time: Lua is single threaded, therefor for the thread calling the yield to be running in the first place the thread it’s trying to yield must already be yielded.
Lua is single-threaded.
Roblox uses a task scheduler to create the illusion of multi-threaded Lua, yielding a coroutine outside of a coroutine is both illogical and impossible.
As tnavarts explains quite nicely, calling coroutine.yield outside of a coroutine would cause you to yield an already yielded coroutine, so you’d basically be doing nothing.
Just fyi you can sort of do it since coroutines have access to the data in the environment it was made in. So you could just have a variable that the coroutine can check to know when to yield.