Task spawn works, but coroutine create doesnt?

Hi, I’m having an issue with coroutines where the function I create doesn’t run. Printing the coroutine as a variable returns the thread address however.

image
image
^ no “1” outputted

image
however this works

Have you used coroutine.resume(name) yet?

local co
co = coroutine.create(function ()
           print("1")
     end)

coroutine.resume(co)

> DevHub Resources <

I found this in the DevHub, meaning that you have to create a function and then use coroutine.create(). Maybe this helps? :thinking:

local function task(...)
	-- This function might do some work for a bit then yield some value
	coroutine.yield("first")  -- To be returned by coroutine.resume()
	-- The function continues once it is resumed again
	return "second"
end

local taskCoro = coroutine.create(task)
-- Call resume for the first time, which runs the function from the beginning
local success, result = coroutine.resume(taskCoro, ...)
print(success, result)  --> true, first (task called coroutine.yield())
-- Continue running the function until it yields or halts
success, result = coroutine.resume(taskCoro)
print(success, result)  --> true, second (task halted because it returned "second")
1 Like

ah this works, thank you.

Nice, please note that once you first create a coroutine, it always start at its “suspended” state. You can change to “running” state by using coroutine.resume(), and change to “suspended” again by using coroutine.yield() once the coroutine finishes its job, it changes to “dead” state.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.