Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by
joeldes 201 Moderation Voter
5 years ago
Edited 5 years ago

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
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. mattscy 3725 — 5y
0
Thanks for the tip. I realized that after I posted it. @mattscy joeldes 201 — 5y
Ad
Log in to vote
1
Answered by
mattscy 3725 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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!

0
Thank you so much dude, will give credit TheBeaver101 28 — 5y
Log in to vote
0
Answered by 5 years ago

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
0
Line 2 is wrong. You use Color3.fromRGB(). User#19524 175 — 5y
0
And pairs is not a loop, it’s a function. User#19524 175 — 5y
0
Also, you may want to iterate through the descendants rather than children with :GetDescendants() mattscy 3725 — 5y
0
thanks so much man! TheBeaver101 28 — 5y

Answer this question