Coloring parts using "GetChildren()"

So I’m making a car script where you buy it it will spawn you a car and stuff, but I’m lazy to get every color part to color the color they wanted, so I tried this and I don’t see the problem here.

				for _, child in pairs(Colors:GetChildren()) do
					if child.Name == "Change" then
						child.Color = Color3.new(1, 0.666667, 0.498039)
					end
				end

It only changed 1 part color, how do I make it so it changes all the others?

(The color variable just points out where the colorparts are at.)

3 Likes

If I remember correctly, :GetChildren() returns an Array, therefore you must use ipairs instead of pairs.

You can read more about it here and here.

1 Like

I tried both of em and uh, just colored 1 part again.

The parts name you want to change is “Change” correct?

1 Like

Are the more parts with the name “Change” or just 1?

1 Like

There are several parts named “Change” that’s what i’m trying to figure out how to color all of them parts instead of just 1.

1 Like

Can you show me what did you put on the color variable ?

1 Like

It just points out where all the color parts are at. there is nun wrong with that

1 Like

Yes but can I see? Because I did this and it worked:

local color = workspace

for _, child in pairs(color:GetChildren()) do
	if child.Name == "Change" then
		child.Color = Color3.new(1, 0.666667, 0.498039)
	end
end
1 Like

Alright, here it is

local Colors = game.ServerStorage.Cars.Sedan.Body.Colors

afbeelding

hmmm, this is weird because I did the same thing and it works for me:

local Colors = game.ServerStorage.Cars.Sedan.Body.Colors

for _, child in pairs(Colors:GetChildren()) do
	if child.Name == "Change" then
		child.Color = Color3.new(1, 0.666667, 0.498039)
	end
end

image

1 Like

See, mine changed it only on 1 part for some reason.
afbeelding

1 Like

Maybe use waitforchild? That might work, maybe the other parts are not added yet?

1 Like

Oh yeah true, let me try that out.

And also, maybe add a wait:

local Colors = game.ServerStorage:WaitForChild('Cars'):WaitForChild('Body'):WaitForChild('Colors')

wait(1)

for _, child in pairs(Colors:GetChildren()) do
	if child.Name == "Change" then
		child.Color = Color3.new(1, 0.666667, 0.498039)
	end
end
1 Like

Wait hold up, where do i add waitforchild, cuz i never used waitforchild in these type of codes.

1 Like

I’ll try using wait() then, give me couple sec

Just do this:

local Colors = game.ServerStorage:WaitForChild('Cars'):WaitForChild('Body'):WaitForChild('Colors')

wait(1) -- change this to whatever you want

It should work.

ohh in the variable, i get it, alright.

using pairs or ipairs it still works normally