How to do Simultaneous Loops

Hi! I have a problem with my script. It has two “for i =” loops, and the 2nd loop will not run unless the first loop is finished. How would I make them run simultaneously? Thank you in advance!

1 Like

Possibly the task.spawn function? Coroutine?

To make each iteration run independently, use task.spawn:

for i = 1, 4 do
    wait(4-i)
    print(i)
end

--[[
    Prints:
    1
    2
    3
    4
]]
for i = 1, 4 do
    task.spawn(function()
        wait(4-i)
        print(i)
    end)
end

--[[
    Prints:
    4
    3
    2
    1
]]
2 Likes

It depends on what your use case is,

task.spawn() / task.defer() is recommended if you plan to run a piece of code along side the Main thread without disruption.

with coroutines, you have more control of the thread, you are able to pause it, resume it, and close it (I think task.cancel() closes the thread with task)

However it is best to note that when the code inside the coroutine is finished or has errored, it is now considered dead by the script running it.

if you want code to execute immedietely, use task.spawn()
if you want code to not execute immedietely, use task.defer()

otherwise, just use coroutines


@GummyScript Already provided the coroutine Documentation
task Library

This doesn’t change anything, task.spawn() is inside the loop. It will still yield the code until the loop finishes, and “might” have a possible lag spike since there is no yielding between iterations of task.spawn().

1 Like

No it won’t? What are you talking about?

2 Likes

There is a Difference between this:

task.spawn(function()
    for i = 1, 4 do
        task.wait(4 - i)
        print(i)
    end
end)

And this:

for i = 1, 4 do
    task.spawn(function()
        wait(4-i)
        print(i)
    end)
end

One yields, the other doesn’t


This would also be technically be yielding as well, but its running alongside the thread.

@bluebxrrybot Basically what I’m trying to say is, since you are putting task.spawn() inside the loop, the main thread ignores the wait() and the print() and continues to run as if it weren’t there.

However, this could just be intended

I wrote this:

for i = 1, 4 do
    task.spawn(function()
        wait(4-i)
        print(i)
    end)
end

You wrote this:

task.spawn(function()
    for i = 1, 4 do
        task.wait(4 - i)
        print(i)
    end
end)

I’m very confused,
It’s probably nothing.

Basically, if you do that, task.spawn() will have the run alongside the main code, so if i did this:

task.spawn(function()
    while true do
        print"E"
        task.wait(.5)
    end
end)

while true do
    print"A"
    task.wait(.5)
end

it would be printing both E and A

but if you did this:

for i = 1,10 do
    task.spawn(function()
        task.wait(.5)
        -- i'm too lazy to add code, fill in the blank
   end)
end
while true do
    print"A"
    task.wait(.5)
end

This would still yield the main thread (as in it will stop the main code until the loop finishes), and it may (it may cause lag but its more unlikely unless you are running a lot) cause lag as it is all running at the same time because the wait is inside the other thread and not within the main one

it may yield the code .5 seconds but it will print all 10 at the same time

These two pieces of code are both correct but both have completely different purposes.

That’s why is said it could be intended

I’m not here to argue, so Send a Message if you have more to say.

Along with task.spawn() as others said, running one loop in coroutine.wrap() should work as well.

Keep this in mind though:

(and also the rest of their post!)


coroutine.wrap(function()
    for i = 0, 10, 1 do
        task.wait(0.25)
        print("A")
    end
end)()

for i = 0, 10, 1 do
    task.wait(0.25)
    print("B")
end

I would personally use task.spawn(), but coroutines work as well.

1 Like

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