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

How do I make this piece of code loop twice using something simple?

Asked by
Peeshavee 226 Moderation Voter
7 years ago

I have this code:

local outter = game.Workspace.all.outter:GetChildren()
local second = game.Workspace.all.second:GetChildren()
local third = game.Workspace.all.third:GetChildren()
local last = game.Workspace.all.last:GetChildren()
local x = 0

while true do

    for i,v in pairs(outter) do
        spawn(function()
        v.BrickColor = BrickColor.new("Really red")
        v.Material = "Neon"
        wait(0.5)
        v.BrickColor = BrickColor.new("Really black")
        v.Material = "SmoothPlastic"
        end)
    end
    wait(0.5)
    for b,n in pairs(second) do
        spawn(function()
        n.BrickColor = BrickColor.new("Really red")
        n.Material = "Neon"
        wait(0.5)
        n.BrickColor = BrickColor.new("Really black")
        n.Material = "SmoothPlastic"
        end)
    end
    wait(0.5)
    for h,j in pairs(third) do
        spawn(function()
        j.BrickColor = BrickColor.new("Really red")
        j.Material = "Neon"
        wait(0.5)
        j.BrickColor = BrickColor.new("Really black")
        j.Material = "SmoothPlastic"
        end)
    end
    wait(0.5)
    for o,p in pairs(last) do
        spawn(function()
        p.BrickColor = BrickColor.new("Really red")
        p.Material = "Neon"
        wait(0.5)
        p.BrickColor = BrickColor.new("Really black")
        p.Material = "SmoothPlastic"
            end)
        end
    wait(0.5)

end

To make it repeat, I tried doing this

local x = 0

while true do
    repeat until x == 2
--code
x = x+1

I thought that if I did this, that once the whole code ran, it would add 1 to x. After it did thise twice so that x == 2, then the code would just stop. But it didn't work. Any ideas? All help is appreciated!

1 answer

Log in to vote
1
Answered by 7 years ago

Use a for loop:

for i = 1,2 do
--code
end
0
This worked! I was able to combine the while true do with the for loop. Thanks! Peeshavee 226 — 7y
Ad

Answer this question