Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make two "for" "do" loops work at the same time?

Asked by
mrcool2 75
9 years ago

In a "for" "do" loop, have can you have more than one set of rules happening at once, for instance having:

'i' decreasing to 0, whilst 'a' increases to 2.

I would have imagined it would look like this, but it does not seem to work:

for i=1, 0, -0.1 do
    for a=1, 0, 0.1 do
        wait(0.5)
        print(i)
        print(a)
    end
end

This is obviously just a scenario, however how do I get them both to count simultaneously on the output?

0
You created a nested for loop, which is very useful for other things later. aquathorn321 858 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

I don't know if this would be the most efficient way of doing this, but you could use coroutines, and it would look something like this:

coroutine.resume(coroutine.create(function() for i = 1,10 do print(i) wait() end end))
coroutine.resume(coroutine.create(function() for i = 1,10 do print("I'm printing!") wait() end end))
0
Coroutines are more effective than the spawn function in this case, because they are executed without delay. aquathorn321 858 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Two different scripts or two different threads; First one is pretty self explanatory so I won't explain that method.

You can create a new thread with the inbuilt spawn function, it takes a function parameter

spawn(function()
    while true do
        wait(1)
        print'Im looping :D'
    end
end)

print'i printed even with a loop above me'

for your case, something like this

spawn(function()
for i = 1, 10 do
print(i)
end
end)

for i = 1, 20, 2 do
print(i)
end

Answer this question