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

How can I make a script that changes the color of all the parts inside a model?

How can I make a script that changes the color of all the contents (parts only) inside a model to a certain ID. I have no idea what I am doing lol.

local model = workspace.FerrisWheelLights2
local color = Color3.fromRGB (0, 255, 0)
local child = model:GetChildren() 
  
child.Color = color

I am clearly not the best scripter but yeh.

3 answers

✓ Accepted answer

Do this

local model = workspace.FerrisWheelLights2
local color = Color3.fromRGB (0, 255, 0)
local child = model:GetChildren() 

for i,v in ipairs(child) do
	v.Color = BrickColor.new(Color3.fromRGB(40,255,255))
end

2 comments

0

Color3 takes a value between 0 and 1 (you're not even using the colour he wants either), and you have to check if each child is a BasePart otherwise it may error. Also, you may want to explain how the loop works too.

0

Thanks for the tip. I realized that after I posted it. @mattscy

You are able to iterate through all of the descendants of the model (which includes every object inside it), and change the colour of each object inside. You must check if each object is a BasePart (which includes Parts, Wedges, Unions, etc.) before changing the colour, otherwise it will error when it comes across an object that doesn't have a Color property. In this example, the for loop will allow you to iterate through the model's descendants, and each iteration the object can be referred to as the variable Part (defined in the loop).

local model = workspace.FerrisWheelLights2
local color = Color3.fromRGB (0, 255, 0)
local descendants = model:GetDescendants()

for _,Part in pairs(descendants) do
	if Part:IsA("BasePart") then
		Part.Color = color
	end
end

Basically, GetDescendants() returns a table of every object inside the model, including children of children and so on, while GetChildren() only gives you the direct children of the model (and not the children of those children).

Hope this helps!

1 comment

0

Thank you so much dude, will give credit

So basically there's a pairs loop you can use with :GetChildren() to get all the stuff in a model

local model = workspace.FerrisWheelLights2
local color = Color3.new(0, 255, 0)

for i,v in pairs(model:GetChildren()) do
	if v:IsA("BasePart") then
		v.Color3 = color
	end
end

4 comments

0

Line 2 is wrong. You use Color3.fromRGB().

0

And pairs is not a loop, it’s a function.

0

Also, you may want to iterate through the descendants rather than children with :GetDescendants()

0

thanks so much man!

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