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

How do I change multiple parts the same color via script/selection?

Asked by 5 years ago
Edited 5 years ago

I've designed a model of a mouse. The mouse has a lot of parts to provide a wide window of customization for the player.

Screenshot of the Finished Model

Screenshot of model contents (put total part count beside folder name just to show viewers, it is not there normally)

I'm wanting to create a script that allows me to change the BrickColor property of all the parts that create the "body" , "secondary" , "skin" and "eye" color.

Most of the parts are union parts if that changes anything.

My longterm goal is to create a script that allows character customization at the start of game. With a variety of different colors that can be chosen from for each area of the creatures body. However, I know that is probably a bit more advanced so ill settle with simply learning how to change the folder contents to the same BrickColor property.

from looking around on the internet i've started with this: local children = workspace.FieldMouse.body:GetChildren()

I can't seem to get much else to work though, I tried an if then statement to detect for a part and then change the property, but it didn't work...and I can't get much further than that.

I'm hoping someone can guide me in the right direction!

1 answer

Log in to vote
0
Answered by
Avigant 2374 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

We can use a numeric for loop to iterate through the array returned by Instance:GetChildren() like so:

local Children = game.Workspace:GetChildren()

for Index = 1, #Children do
    local Child = Children[Index]

    -- Here we can do something for every child, but first we need to check if it's a part.
    if Child:IsA("BasePart") then
        Child.Color = Color3.new(0, 0, 0)
    end
end

BasePart is called the superclass all the possible types of parts, sort of like how objects in the game have parents and children, BasePart is the parent of unions, mesh parts, wedge parts, normal parts, and so on.

We simply iterate through an array with a numeric for loop, starting at 1, and going up to the length of the array. For each iteration, we get the child in the table by looking up the numeric key in the array (Table[Key] gets the value at Key in Table).

If you'd like to know more about numeric for loops, check out the Roblox Wiki entry for them.

0
I'm confused by the first line, how does the script know to look in the specific model file? Would I change it to " local Children = game.workspace.FieldMouse.body:GetChildren() " in order to get all children within the parent folder "body" ? sorry for being such a newb, aha CaityKo 5 — 5y
0
Yep, you just define a path to what you want, just like that! It's case sensitive though. Avigant 2374 — 5y
0
AH, Yes! it changed all specific parts I wanted! But, when I input (0, 255, 0) for testing it shows up a solid black color rather than the green I specified. Did I do something wrong? " Child.Color = Color3.new(0, 255, 0) " CaityKo 5 — 5y
0
That's because Color3 components are from 0 to 1, not 0 to 255. In your case, you can use Color3.new(0, 1, 0). In general, you can either divide your 0-255 number by 255, or replace Color3.new() with Color3.fromRGB(), which takes 0-255 components. Avigant 2374 — 5y
0
Beautiful! Thank you so much! CaityKo 5 — 5y
Ad

Answer this question