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

Is there a way to put a while true do loop inside of a for i, v in pairs loop?

I am having trouble with my script. I want to make everything inside a model named, "Boxes", turn to a random color each .5 seconds. But my current script only changes one part inside the model.

script.Parent.ClickDetector.MouseClick:Connect(function()
	
	script.Parent.BrickColor = BrickColor.Green() --changes button color
	
	for i, v in pairs(workspace.Boxes:GetChildren()) do 
		
		if i == 3 then --checks to see if i has looped 3 times (which equals the # of boxes inside the model, "Boxes")
			
			while true do --should do a random color loop
				
				wait(.5)
				v.BrickColor = BrickColor.Random()
				
				wait(10)
				break
				
			end
			
		end
				
	end
	
	for i, v in pairs(workspace.Boxes:GetChildren()) do --changes boxes back to original color
		
		v.BrickColor = BrickColor.new("Medium stone grey")
		
	end
	
	script.Parent.BrickColor = BrickColor.Red() --changes button color
	
end)

I'm not sure if i did the wait(10) break thing right but that's not what I'm asking y'all to fix. I'll fix that part on my own.

3 comments

0

Yep, I didn't do the wait thing right. If you're planning on helping me answer my question, just ignore lines 14-15, 23-29

0

Why would you want to put an infinite loop inside a for loop? It would just keep running the same code on the first index forever.

0

Cuz I'm tryna make the parts inside a model all change colors randomly in a loop after the for i, v gets the parts inside the model

1 answer

✓ Accepted answer

Heres some code i wrote works fine for me!

GIF

LAYOUT IN EXPLORER


script.Parent.BrickColor = BrickColor.Green()
local debounce = false

script.Parent.ClickDetector.MouseClick:Connect(function()
	if debounce == false then -- this debounce is to stop ppl from spamming
		debounce = true
		script.Parent.BrickColor = BrickColor.Red()
		wait(.5)
		for i,v in pairs(game.workspace.boxes:GetChildren()) do
			wait(.5)
			if v:IsA("Part") then -- just sanitizing 4 no errors
				v.BrickColor = BrickColor.Random()
			end
		end
		wait(.5)
		for i,v in pairs(game.workspace.boxes:GetChildren()) do
			wait(.5)
			if v:IsA("Part") then -- just sanitizing 4 no errors
				v.BrickColor = BrickColor.new("Medium stone grey")
			end
		end
		wait(.5)
		script.Parent.BrickColor = BrickColor.Green()
		debounce = false
	end
end)

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