How to change the color of all parts with the same names?

Hello everyone! I wrote a script that, when the button is pressed, creates a new part and changes its color to black, but if I create a lot of parts, then the color changes only for the first one, is it possible that the color would change for all parts? Here is my script

local cd = script.Parent.ClickDetector

function onclicker()
	local blackbor = Instance.new("Part")
	blackbor.Name = ('A')	
	blackbor.Anchored = false
	blackbor.Shape = Enum.PartType.Ball
	blackbor.Parent = game.Workspace
print('clicked')
	
end
cd.MouseClick:connect(onclicker)
while true do
	local  b = game.Workspace:WaitForChild('A')
wait(1)
	b.BrickColor = BrickColor.Black()
end

Just reference the part you made and change that color

local cd = script.Parent.ClickDetector

function onclicker()
	local blackbor = Instance.new("Part")
	blackbor.Name = ('A')	
	blackbor.Anchored = false
	blackbor.Shape = Enum.PartType.Ball
	blackbor.BrickColor = BrickColor.Black()
	blackbor.Parent = game.Workspace
	print('clicked')
end

cd.MouseClick:connect(onclicker)

You could make a for loop, then check if the current part in the loop has the same name as the name of the part, if it does you change it.
https://education.roblox.com/en-us/resources/repeating-tasks-with-for-loops
If you’re placing in a loop I’d suggest not adding inside the for loop, as it will make it so the parts don’t change at the same time.

1 Like

This would change the parts to a random color every second, but you could just put in the specific color you want inside of the for loop.


local folder = Instance.new("Folder", workspace)

local cd = script.Parent.ClickDetector

function onclicker()
	local blackbor = Instance.new("Part")
	blackbor.Name = ('A')	
	blackbor.Anchored = false
	blackbor.Shape = Enum.PartType.Ball
	blackbor.Parent = folder
print('clicked')
	
end
cd.MouseClick:connect(onclicker)
while true do
wait(1)
	for i, part in pairs(folder:GetChildren()) do
          part.Color = Color3.fromRGB(math.random(1,255), math.random(1,255), math.random(1,255))
    end
end
1 Like

Thanks, so much!

(30 letters)

1 Like

No problem, glad to help :slight_smile: !