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

How can i activate a for loop while there's a for loop running?

Asked by 5 years ago

So if i have a for loop running at the moment, how can i activate another for loop, in that same script, while the other for loop is running?

3 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago
spawn(function()
    for i,v in (i)pairs(array) do
        --// Functionallity
    end
end)

for i,v in (i)pairs(array) do
    --// Functionallity
end

Spawn creates a separate thread for the piece of Lua C to run in, while the rest of the opposite thread is running alongside, being unaffected by the lone program.

0
Anytime you want something to run at the same time the rest of the Script is running it's thread (Anything inside of it really) just write that and put it inside of it, Spawn creates a separate thread for the program to run on it's own, alone. Almost like creating another Script for it to run into User#25448 0 — 5y
0
A thread is the piece of Code written into a Statement, anything inside of a Script is one complete thread, anything inside of Eg. an If Statement, it'll still be counted for as inside of the Scripts thread family, but also in the if Statements family. It's hard to explain, but you don't even need to know how Spawn works, just write whatever you want to run separate at the same time in there User#25448 0 — 5y
0
and you'll be fine, also if this helped check it being answered:3 every little bit helps me too User#25448 0 — 5y
0
Wow, never knew about this, thanks for the help! nikki9dor 28 — 5y
0
Yeah, anytime for a fellow questioner, we all need help. no matter how smart we think we are User#25448 0 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

on the loop spawn(function() loop end) meaning it'll run and the script will continue running even if the loop hasn't ended

https://developer.roblox.com/articles/Thread-Scheduler

0
Um, not understanding. nikki9dor 28 — 5y
Log in to vote
1
Answered by
8391ice 91
5 years ago
Edited 5 years ago

There are two ways to accomplish this!

1. Create 2 scripts with for loops and activate them both from another script! This obviously isn't ideal, so I would recommend...

2. Include the second for loop in a separate function! Take a look at the example script below:

function secondLoop() --This is the second for loop in a separate function!
    for j = 1, 10 do
        print(j)
    end
    wait()
end

for i = 1, 20 do
    if i == 10 then --This will ONLY fire when i reaches 10, which is about halfway through the script
        secondLoop() --Now, another for loop has started while this one is running!
    end
    print(i)
    wait()
end

This, I would say, is the simplest way to achieve what you're asking for. If you have any questions, please feel free to ask!

Answer this question