How do I make every part with a certain name a random color?

This is a support category for asking questions about how to get something done on the Roblox websites or how to do something on Roblox applications such as Roblox Studio.

A few days ago, I made a city map, with several grouped buildings. In these buildings, I have further divided them into sections of the building, with window panes and window lights.
What I’m trying to do is, every few minutes, make these dark window lights have a 50/50 chance of becoming “Really Black” or “Institutional White”

I already set the material of these light blocks to Neon,
image

Since I’m a very inexperienced developer, I don’t really know how to achieve this sort of thing.

I have tried to use :GetDescendants() to achieve this, but I don’t think I made the script the right way.

This is the script that I made, with the help of some tutorials; It is a child of the Workspace and I am trying to make it look for any descendant of the workspace with the name “WindowLight”, but it does not work.


local lights = script.Parent:GetDescendants("WindowLight")

for _, step in ipairs(lights) do
	if step:Isa("Part") then
		step.BrickColor = BrickColor.new("Cool yellow")
		
	end
end



you can use math.random()
if you do math.random(1,5) it will randomly select numbers from 1 to 5 (including 1 to 5)

for _, step in ipairs(lights) do
	local Number = math.random(1,2)
	if step:Isa("Part") then
		if Number == 1 then
			step.BrickColor = BrickColor.new("Black")
		elseif Number == 2 then
			step.BrickColor = BrickColor.new("Cool yellow")
		end
	end
end
1 Like

Here is a working example:

local lights = script.Parent:GetDescendants()

for _,step in ipairs(lights) do
	if step:IsA("Part") and step.Name == "WindowLight" then
		step.Color = Color3.fromRGB(math.random(0,255),math.random(0,255),math.random(0,255))
	end
end
1 Like

Use a dictionary + math.random(). That way you can assign strings colors at ease. Also, nice building.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.