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

Why is my for loop only printing 1 part out of the multiple in the folder?

Asked by 3 years ago

this is my script

local script = script.Parent
local folder = script:GetChildren()
for _, v in pairs(folder) do
    if v:IsA("MeshPart") then
        print("hi")
        while true do
            wait(.2)
            v.Color =   Color3.fromRGB(math.random(1,255),math.random(1,255),math.random(1,255))
        end
    end
end

So my script works perfectly fine, other than only 1 "hi" is printed and only 1 part does the color changing. The MeshPart that is changing colors is also not the first child in the Model.

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

It's because the while true do stops the whole entire for loop. It just focuses on what's inside the while loop and not outside it

What I recommend you doing is making a seperate Script for the changing color. Then put it to someone then name it to anything, AND BE SURE TO DISABLE IT. For now, let's name it "ColorChange"

-- Color Change Script:

while true do
            wait(.2)
            script.Parent.Color =   Color3.fromRGB(math.random(1,255),math.random(1,255),math.random(1,255))
end

Then in your main script

-- Main Script:

local script = script.Parent
local folder = script:GetChildren()

local colorChangeScript = wherever.you.put.it.ColorChange

for _, v in pairs(folder) do
    if v:IsA("MeshPart") then
        print("hi")

        local colorChangeScriptClone = colorChangeScript:Clone()
        colorChangeScriptClone.Parent = v
        colorChangeScriptClone.Disabled = false
    end
end

If it helped, be sure to accept it as an answer :D

0
Thanks for the help! BenTylka 2 — 3y
0
you're welcome! TheBigBro122 427 — 3y
0
The pro scripter arises Xyternal 247 — 3y
Ad

Answer this question