Is there any way to run code at the same time in single script without coroutines?

Hello. I’m have 1 small problem, which breaking my game - IDK how to run my for loop x times without waiting until previous looping cycle finished.
Example: I have 3 balloons, which I want to inflate at the same time. But using for loop, only 1 balloon can inflate in the same time. How I can run for loop multiple times, without yielding, if loop have wait(x)?

Why can’t you use coroutines?

for ... do
    coroutine.wrap(function()
        InflateBalloon()
    end)()
end
1 Like

I have 1 another question:
Does coroutine.wrap(function) works the same as coroutine.resume(coroutine.create(function))?

It should, but it’s shorter.

1 Like

At a base level they do, but coroutine.wrap returns a wrapper function, a function value wrapped around a thread value, when that function is called the thread is resumed. coroutine.create on the other hand creates a thread value and coroutine.resume resumes it.

coroutine.resume(coroutine.create()) is thread safe as the coroutine’s task (function) is ran in an alternate function call stack, any errors that may arise are not propagated but they will cause the alternate call stack to be terminated. coroutine.wrap() on the other hand is not thread safe as its task (function) is ran in the current (active) function call stack, any errors that may arise will be propagated to the call to the function value returned by ‘coroutine.wrap()’ causing the current (active) stack to be terminated.

coroutine.wrap(function()
	error("Error!")
end)()

print("Hello world!") --Not printed as error in 'coroutine.wrap' terminates the current (active) function call stack.
coroutine.resume(coroutine.create(function()
	error("Error!")
end))

print("Hello world!") --Is printed as error in 'coroutine.resume(coroutine.create))' does not terminate the current (active) function call stack but instead terminates the alternate stack created for it.
2 Likes