Coroutine Close / Task Cancel Help

I am trying to pause/destroy a coroutine from outside the coroutine and it keeps giving me an error. I dont use coroutines alot so I’m not sure what I’m doing wrong.

I’ve made a new place file and made this minimum reproducible error. It errors everytime, no matter if I use coroutine.resume(coroutine.create()), task.spawn, etc. It also errors if I try coroutine.close. There is no other code in this placefile other than these 7 lines. I’ve also tried putting waits to see if I was just closing it too fast and it still didn’t work.
image

Is anyone else having the same error?

local t = task.spawn(function()
	for i = 1, 100 do
		wait(1)
	end
end)


wait(1)
task.cancel(t)
1 Like

It’s actually because the wait will wait for 1 second then try to resume the thread, but it has been canceled (dead). Using task.wait seems to remove this error message.

wait uses lua’s coroutines where task.wait uses roblox’s, it’s best to use the task. alternatives and it’s more important not to mix them.

2 Likes

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