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

I have a simple question about parts with the same name?

Ok so I know this may sound like a dumb question but I have parts with the same name that I want to paint all at the same time on button press because renaming all of the parts can be tiresome and will create a longer script, I've come up with a script but it only works for one part of course because there are many of the same parts named "Wall," I've looked at scripts for calling all of the parts at the same time but I can't figure out how to do it, can anybody demonstrate on my script below?

Button = script.Parent
function OnButtonClick()
	game.workspace.Wall.BrickColor = BrickColor.new(192)
	end

script.Parent.MouseButton1Down:connect(OnButtonClick)

2 answers

You want to do something for each brick named "Wall" in the workspace; this calls for a for loop.

A for loop does a block of code for each object in a collection.

We will look at for each child in the workspace's list of children, if that child.Name == "Wall", we will set its .BrickColor:

for _, child in ipairs(workspace:GetChildren()) do
	if child.Name == "Wall" then
		child.Wall.BrickColor = BrickColor.new(192)
	end
end

I suggest using BrickColor.new("Reddish brown") instead of referring to it by 192 so that you can remember what the color is when you come back to this script in a few days!

2 comments

0

Thank you! Does this work with a function OnButtonClick() because I just tried to add it to my code and it didn't work

0

I think it's better practice to usually use pairs, and i,v if you are not 100% sure what the objects are

Use a for loop, for loops iterate through a table (in this case, GetChildren())

So if you wanted to rename all parts named "Wall" you would do

for _, c in in pairs(workspace:GetChildren()) do
	if c:IsA("BasePart") and c.Name == "Wall" then
		game.workspace.Wall.BrickColor = BrickColor.new("Reddish brown")
	end
end

What it does is go through each object in the workspace, it checks if it is a part first, then if it is named "Wall" it will set its colour to Reddish brown (192)

2 comments

0

How would I make this a function OnButtonClick()

0

Just put it inside

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