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

How do I use coroutine.yield()?

Asked by
glaqss 20
3 years ago
Edited 3 years ago

I am wondering how to use coroutine.yield() because i can't figure out how it works. I have tried to understand the article on the Roblox's own devhub, but I just can't understand it. Can anyone help?

Code:

function Alarms.On(Color)
    for i, v in pairs(Alarms.AlarmTable) do
        v.Activate(Color) -- Activates a bunch of smaller scripts
        coroutine.yield() -- Tries to yield but fails
    end
end

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Basically coroutine.yield() will pause the script so nothing continues until what is inside of the () is completed. You appear to be using it wrong in your script, an example of how I would use it is below.

local num = 0;

while true do
    wait();
    print("Adios: " .. num);
    coroutine.yield(num += 1); -- This acts as a wait() but instead of passing a number it waits until whatever I put inside it is completed. Once complete it will continue the script.
end

(If you have previously coded in C#, JS, etc. You will find that you can use await inside of async functions coroutine.yield() is like an await but can be used anywhere and is a lua version of await.)

0
Sorry, but this didn't work, and I know why it won't.. It's activating a bunch of outside scripts and it simply waits for them to finish, which is unfortunaley not gonna work. glaqss 20 — 3y
Ad

Answer this question