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

Trying to run 2 separate for loops at the same time?

Asked by 6 years ago
Edited 6 years ago
function module.gamemode()
    for timer = timeLimit, 0, -1 do
        spawn(function()
            for _, map in pairs(workspace:WaitForChild("Castle Map"):GetChildren()) do
                if map:FindFirstChild("Flag") then
                    local color = map.Flag.BrickColor
                    if color == game.Teams.Red.TeamColor then
                        redFlags = redFlags + 1
                    elseif color == game.Teams.Blue.TeamColor then
                        blueFlags = blueFlags + 1
                    end
                end
            end 
            wait(3) 
        end)
        wait(1)
    end
end

I'm trying to run 2 seperate for loops at the exact same time, but to have each of them wait a different amount of time. What I want is to have the main for loop running every second, as this is the game countdown clock. However, the for loop inside the spawn() function needs to loop every 3 seconds. Atm, both for loops run off a second, so the team gets + 1 every second, instead of every 3 seconds

Script is inside a ModuleScript as well

1 answer

Log in to vote
1
Answered by 6 years ago

No need to have two loops running at the same time if you only want one loop to run every 3 seconds.

Instead, you can use an accumulator variable to keep track of how many seconds have passed.

An accumulator variable is useful for when you want to track the amount of *something *in a loop, whether it be seconds passed or an amount of a specific string.

In this case you're wanting a loop to run every 3 seconds. This can be achieved with the accumulator variable and the modulus operator, by checking when the accumulator has reached a multiple of 3.

Modulus Information: http://wiki.roblox.com/index.php?title=Arithmetic_operator

*If the starting time ended with 0, an accumulator variable would not be needed. But for versatility, we'll do it this way. *

function module.gamemode()
    local accumulator = 0 -- Keeps track of seconds passed

    for timer = timeLimit, 0, -1 do -- Timer Loop
        if accumulator % 3 == 0 then -- Checking if 3 seconds have passed
            for _, map in pairs (workspace:WaitForChild("Castle Map"):GetChildren()) do
                if map:FindFirstChild("Flag") then
                    local color = map.Flag.BrickColor

                    if color == game.Teams.Red.TeamColor then
                        redFlags = redFlags + 1
                    elseif color == game.Teams.Blue.TeamColor then
                        blueFlags = blueFlags + 1
                    end
                end
            end
        end

        accumulator = accumulator + 1 -- Adding onto the accumulator variable

        wait(1) -- Waiting 1 second
    end

If you have any questions, feel free to ask :)

0
Works, thank you :D NinjoOnline 1146 — 6y
Ad

Answer this question