✓ Accepted answer
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.
local model = game:GetService("Workspace").Parts
local clickButton = game:GetService("Workspace").click
local function clickToChangeColour ()
for _, child in pairs(model:GetChildren()) do
child.BrickColor = BrickColor.Red()
end
end
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.
2 comments
Thank you for your help!
^ You are encouraged to mark the question as answered if it helped you.