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

How to run multiple loops at the same time?

Asked by 8 years ago
for q=1,100 do
wait()
    print(q*2)
end

for i=1,200 do
wait()
    if not i%2 == 0 then
        print'not EVEN close' --I know, not very punny, oops, funny
end
end

What's going to happen is the first loop is going to run, and then once it's over, the next loop will run. The only way(in my mind) is to make a separate script for the second loop. But i need 6 loops to run at the same time, and I don't want 6 scripts appearing whenever the round restarts(i need 6 loops to run when the round restarts for various purposes). So how can i get 6 loops to run at the same time in the same script?

4 answers

Log in to vote
2
Answered by 8 years ago

I'd say you have to make it so something triggers them with a function, that's how I'd do it at least.

--Example idea
game.Workspace.ChildAdded:connect(function()
for q=1,100 do
wait()
    print(q*2)
end
end)
game.Workspace.ChildAdded:connect(function()
for i=1,200 do
wait()
    if not i%2 == 0 then
        print'not EVEN close' --I know, not very punny, oops, funny
end
end
end)
0
umm. How does that work with 6 loops in a script? ScriptsAhoy 202 — 8y
0
You'd make it so, say when the round starts, you'd make a function based on triggering when that happens. If you don't understand how functions work: http://wiki.roblox.com/index.php?title=Function Vingam_Securis 213 — 8y
0
No, i mean how do i get 6 loops to run at the same times in a script. Because the script will finish one loop, then go onto the next, how do i get it to do all of them at the same time in one script ScriptsAhoy 202 — 8y
0
Functions, have each function trigger from the same time, thus they'll all run in sync. [At least, as close to sync as you want to make them.] Vingam_Securis 213 — 8y
View all comments (2 more)
0
Can you make a demonstration? ScriptsAhoy 202 — 8y
0
Editted the answer above. Vingam_Securis 213 — 8y
Ad
Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
8 years ago

You can use spawn. This will cause the function you give it as an argument to run separate from the rest of the script (although it's actually much more complex than that, read the wiki about it if you want). This allows you to run two loops at the same time.

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

for i = 1,10 do
    wait(1)
    print(i)
end

You can also use coroutines to get a similar effect. The wiki has pretty much your exact example on it, so I won't bother writing one.

0
Thanks! ScriptsAhoy 202 — 8y
0
How do i use a function with parameters with this? like function add(n1,n2) return n1+n2 end spawn(add(1,2)) is what I'm guessing, will that work? ScriptsAhoy 202 — 8y
0
This is actually addressed on the wiki. You actually can't do that because spawn() takes a function as an argument. 'add()' is not a function, it's the result of the function 'add'. You could do spawn( function() add(1,2) end ) or you could just use corountines. Perci1 4988 — 8y
Log in to vote
1
Answered by
lucas4114 607 Moderation Voter
8 years ago

use coroutine, it lets you run more than 1 things on a script: http://wiki.roblox.com/index.php?title=Function_dump/Coroutine_manipulation

coroutine.resume(coroutine.create(function() --the script will be running both this and the other loop at the same time
    for q=1,100 do
        wait()
        print(q*2)
    end
end))

for i=1,200 do
    wait()
    if not i%2 == 0 then
        print'not EVEN close' --I know, not very punny, oops, funny
    end
end

and you could do:

coroutine.resume(coroutine.create(function() 
    for q=1,100 do
        wait()
        print(q*2)
    end
end))

coroutine.resume(coroutine.create(function() --now both loops are in a coroutine, so if you added anything else it would run both loops and whatever other code you put
    for i=1,200 do
        wait()
        if not i%2 == 0 then
            print'not EVEN close' --I know, not very punny, oops, funny
        end
    end
end))

And if you need to script to run more than 3 things at once you can just add more coroutines!

Log in to vote
1
Answered by 8 years ago

Recursive solution to making a loop and printing it 100 times

function PrintvalTimes(value)
if(value == 0) then --base case
print(value);
else
    PrintvalTimes(value-1)
end


end
PrintvalTimes(100)

then if you wanted another loop, just use this function as a model

0
Thank you, but there were already 3 different answers. ScriptsAhoy 202 — 8y
0
Yes, I know, I was just trying to show ya how recursion works, because I have an exam for recursion tommarow... Also, its very handy to use recursion. scottmike0 40 — 8y
0
o-o-oh, thank you ScriptsAhoy 202 — 8y

Answer this question