Creating and running a coroutine should be as simple as this, correct?

local addCo = coroutine.wrap(function(a, b)
	print(a + b)
end)

addCo(2, 4)
addCo(3, 6)

How can I make this coroutine not die. (coroutine.yield doesn’t work.) I like the implementation of this, which is why I want a coroutine function to be able to be ran multiple times.

You can’t stop it from dying. What you can do is create a function that wraps and runs a new coroutine, then call that function multiple times and passing all the arguments you need.

1 Like

You can use a BindableEvent to have a reusable coroutine.

You could do this:

local addCo = coroutine.wrap(function(a, b)
	while (true) do
		print(a + b)
		coroutine.yield()
	end
end)

addCo(2, 4)
addCo(3, 6)

… But it’s pretty dumb to do in this trivial case :slight_smile:

It seems this was the best way to implement it.

local add = function(a, b)
	print(a + b)
end

coroutine.wrap(add)(1, 3)
coroutine.wrap(add)(4, 3)

I really liked the other functionality, looked much cleaner.