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

How to change a model's color using :GetChildren() and another table?

I'm pretty new at coding. I've been using this ROBLOX game to help me sharpen my skills. This game teaches the basics of Lua and then provides some projects for you to complete. I'm currently stuck on one of these projects.

The project wants you to: Make a loop that changes all the colors in a model to one color every few seconds. Put the list of colors in a table, and have it cycle through the colors in the same order.When it reaches the last color, it will restart and cycle through again. Get all the children from the model with the GetChildren() method and iterate through them with a for loop.

I've tried to complete this project myself but keep failing. The best result I've got is getting a single part to change color and then another part to change color and so on and so on until the parts have cycled through the table of colors.

sp = script.Parent

function LIGHT_IT_UP()
	local c = {BrickColor.Red(), BrickColor.Blue(), BrickColor.Green(), BrickColor.Yellow()
	}
	local m = sp:GetChildren()
	
	while true do
		for t = 1, #c do
			for i,v in pairs(m) do
				if v:IsA("Part") then
					v.BrickColor = (c[t])
					wait(1)
				end
			end
		end
	end
end

game.Players.PlayedAdded:connect(LIGHT_IT_UP)

Hope you lads can help me out.

2 comments

1

You are waiting per part not per colour.

0

You're right, thanks!

1 answer

Wait(1) makes your code wait 1 second after one part's color has been changed

while true do
	for t = 1, #c do -- iterates through all colors in table
		for i,v in pairs(m) do -- iterates through all parts >one by one<
			if v:IsA("Part") then
				v.BrickColor = (c[t]) -- changes color of 1 part
				wait(1) -- after changing color code waits 1 second and goes to start (iteration through parts)
			end
		end
	end
end

You should move wait(1) to the next line after the iteration through all parts:

while true do
	for t = 1, #c do -- iterates through all colors in table
		for i,v in pairs(m) do -- iterates through all parts >one by one<
			if v:IsA("Part") then
				v.BrickColor = (c[t]) -- changes color of 1 part and instantly goes to start (iteration through parts)
			end
		end
		wait(1) --after all part's colors have been changed waits 1 second and then goes to start (iteration through colors)
	end
end

1 comment

0

Thanks, mate.

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