What is spawn and coroutine?

Coroutines die after they’re done running, so you can only coroutine.resume() them once. After that, you have to recreate the coroutine.

You can stop this by using a loop and coroutine.yield():

local thread = coroutine.create(function()
    while true do
        print("hello")
        coroutine.yield()
    end
end)

coroutine.resume(thread) --> hello
print(coroutine.status(thread)) --> suspended
coroutine.resume(thread) --> hello

This way, the coroutine doesn’t die after you coroutine.resume() it. Instead, it goes into a “suspended” state (that’s literally what it’s called) which makes it stop running until you coroutine.resume() it again.

The while loop doesn’t make it run multiple times, it just stops it from dying after it runs once.

1 Like