Some coroutine help? (How do I stop a coroutine from dying?)

So I have a simple combo script, and I’m using coroutine instead of spawn. For better performance.

Coroutine
local breakCombo = coroutine.wrap(function()
	wait(1.2)
	combo = 1
	print("Broke combo!")
	coroutine.yield()
end)

-- alternate one as well with a parameter
local breakCombo = coroutine.wrap(function(combo)
	if combo == combo then
		wait(1.2)
		combo = 1
		print("Broke combo!")
		coroutine.yield()
	else return end
end)

I’m only able to use this function once, and I thought yielding would prevent it from dying.
So my main question is, how can I use this function multiple times throughout my script?

1 Like

You can use coroutine.resume()

Like this?

coroutine.resume(breakCombo(2))

Output:
“Broke combo”
cannot resume dead coroutine

1 Like

Try using coroutine.create() instead, (You will have to do coroutine.create(function() and then coroutine.resume() it after)

And seems like it is giving a error inside the coroutine, the state of a coroutine being dead is returned once the function inside the coroutine gives a error.

local breakCombo = function()
	wait(1.2)
	combo = 1
	print("Broke combo!")
	coroutine.yield()
end
coroutine.wrap(breakCombo)() -- Start 1 coroutine
coroutine.wrap(breakCombo)() -- Start another coroutine

local breakCombo = function(combo)
	if combo == combo then
		wait(1.2)
		combo = 1
		print("Broke combo!")
		coroutine.yield()
	else return end
end
coroutine.wrap(breakCombo)(arg1) -- Start 1 coroutine with param
local cor = coroutine.wrap(breakCombo) -- Start another coroutine with param/ different style
yielded_ret = cor(arg2)
4 Likes

Try working with coroutine.create()

1 Like

I spent an embarrassingly long time on this, thank you. :grinning:

2 Likes