Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

3 comments

0

Thanks for the help!

0

you're welcome!

0

The pro scripter arises

This discussion is preserved as it was. New answers, comments, and votes are closed.