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

Question about coroutines?

Asked by 10 years ago

I have just learned about coroutines, and i'm curious... could I revive a coroutine? I tryed this; and it says that the coroutine is dead.

local p = coroutine.wrap(function()
    print(1)
end

local c = coroutine.wrap(function()
    print(2)
end

while wait(1) do
    p()
    c()
end

2 answers

Log in to vote
0
Answered by 10 years ago

Coroutines are functions, but not meant to be ran like one. The purpose of a coroutine is to be able to run two parts of code at once, and to have better control over when to end it. For example, while loops stop the code from running until it's broken. We can get around this-

x = coroutine.wrap(function()

while wait(1) do
print(":)")
end

end)

wait(1)
Instance.new("Message",Workspace).Text = "Messag'd!"

This would begin the loop, wait a second, then display a message in Workspace.

You can stop this at any moment by saying- coroutine.yield(x)

... that would stop the loop from running.

To run it again, coroutine.resume(x)

Hope I could be of help!

Ad
Log in to vote
-2
Answered by
RedCombee 585 Moderation Voter
10 years ago

In order to revive a coroutine, you have to use:

coroutine.resume()
0
This does "p" and "c" once, then it stops. connor954 20 — 10y

Answer this question