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

How can I change multiple part colors?

Asked by 6 years ago

I'm very new to scripting and when I use the script below it only changes one of the parts. They are all named the same and grouped together. Thanks for helping!

function Colour()
    script.Parent.Parent.Light1.BrickColor = BrickColor.Red()
end

script.Parent.ClickDetector.MouseClick:connect(Colour)

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Hi, 21Bolts. I will be helping you regarding the topic, "How can I change multiple part colors?"

The answer below me works but it is oversimplified for such a simple code. I will explain you what is happening. Be sure to check out the links listed below the code to learn more about them. So lets get started!

To get all the parts inside a model, we use model:GetChildren()

To loop through the parts found in the descendant, we will use for loop, particularly, for i, v in pairs do. The i and v are short for index, and value. This is the loop you would use to cycle through what we call the tables

When do we use loops?

When there are one or more objects in the workspace and we want to change both of their properties in once, instead of repeating the code you wrote the x amount of times. Basically it is a life saver.

model = game:GetService("Workspace").modelName
modelChildren = model:GetChildren()

^ This above code gets all the parts found inside the modelName.

Here is the working code.

-- Declaration Section 

local model = game:GetService("Workspace").Parts
local clickButton = game:GetService("Workspace").click

-- Processing Section

local function clickToChangeColour ()
    for _, child in pairs(model:GetChildren()) do
        child.BrickColor = BrickColor.Red()
    end
end

-- Connecting Section 

clickButton.ClickDetector.MouseClick:Connect(clickToChangeColour)

What the above code does is, the for loop cycles through the model's children and changes the properties. In your case, it is going to change the brick's properties.

It is always suggested to learn more about the loops rather than just copying the above code. Here are few links you might want to check out for better understanding.

0
Thank you for your help! 21Bolts 2 — 6y
0
^ You are encouraged to mark the question as answered if it helped you. Axceed_Xlr 380 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You need to get all the Lights and in order to do that you'll need to use a table.

local ParentOfAllTheLights = workspace -- or where all your lights are
local myTable = ParentOfAllTheLights:GetChildren() --This will form your table

function Colour()
    --Use a for loop to check each item in the table
    for _,part in pairs(myTable) do
        --Only change the brickcolor if it's actually a Part otherwise you may get an error
        if part:IsA("BasePart") then
            part.BrickColor = BrickColor.Red()
        end
    end
end

script.Parent.ClickDetector.MouseClick:connect(Colour)
0
You are oversimplifying the code. How is a new scripter supposed to understand your code. Judging from his description, he wants to click ONE part to change OTHER parts colour. So the solution should not be this complex at all. Axceed_Xlr 380 — 6y
0
Yeah I have no clue what you mean. Sorry for being bad. I want to do the one button to change other part colors 21Bolts 2 — 6y
0
Ahhh I see. I'll update my answer MooMooThalahlah 421 — 6y

Answer this question