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

Question about coroutines?

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

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!

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

coroutine.resume()

1 comment

0

This does "p" and "c" once, then it stops.

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