Is there a way to make everything called (example: Part) turn red or something?

Hello Devs,

Is there a way where I can pick up all the thing named LightIDK and SurfaceLight turn the same colour? Note that there is more than one of each.
Please can you not recommend to change anything but my problem as I am trying to make a alarm system for my scp game.

WARNING: The code might be a bit messy.
My code:
I need help doing the SurfaceLight and LightIDK, they are parts. Sorry for the weird names.

local Mutton = script.Parent.Parent
local CD = script.Parent
local Lght = game.Workspace:FindFirstChild(“SurfaceLight”)
local Part = game.Workspace:FindFirstChild(“LightIDK”)
local Soud = game.Workspace.CodeRed.ProximityPrompt.Script.CodeRedAlarm
local AlarmOn = false

function AlarmIsOn()
AlarmOn = true
Soud.Volume = 5
Soud.Looped = true
Soud:Play()
Lght.Color = Color3.fromRGB(255, 0, 4)
Part.Color = Color3.fromRGB(255, 0, 4)
end

function AlarmTurnsOff()
AlarmOn = false
Lght.Color = Color3.fromRGB(255, 255, 255)
Part.Color = Color3.fromRGB(163, 162, 165)
Soud.Looped = false
Soud:Stop()
end

CD.Triggered:Connect(function()
if AlarmOn == true then
AlarmOn = false
Lght.Color = Color3.fromRGB(255, 255, 255)
Part.Color = Color3.fromRGB(163, 162, 165)
Soud.Looped = false
Soud:Stop()
else
AlarmOn = true
Soud.Volume = 5
Soud.Looped = true
Soud:Play()
Lght.Color = Color3.fromRGB(255, 0, 4)
Part.Color = Color3.fromRGB(255, 0, 4)
end
end)

I tried to do :FindFirstChild, but it didn’t work.

Thank you to who ever solves this!

Bye!

You can use for i, v in pairs() to iterate through the workspace, then check if the part is named “SurfaceLight” or “LightIDK” by using if v.Name. You can then change the colour by using v.Color .

1 Like

Okay, ill try now and then say if it works.

Simple way to do what is needed would be to do this

for _,light in pairs(workspace:GetDescendants()) do
	if light.Name == "SurfaceLight" then
		light.Color = --Color here
	elseif light.Name == "LightIDK" then
		light.Color = --Color here
	end
end

Loop through everything in workspace and if their name is either SurfaceLight or LightIDK, do something. IF you just want to loop through all the children instead of descendants, you could do GetChildren() instead of GetDescendants()

I might of not made it clear in the post but this is the workspace: LIGHTTHING
The surface light is the child of the LightIDK

Then GetDescendants() would work for your needs

1 Like

If you don’t understand in pairs loop here is a tutorial I used to learn about them.

While other have already suggested using GetDescendants(), which will work, it isn’t very scalable. The larger your game becomes, the heavier and more problematic iterating through all descendants will become. I would instead recommend using something like Collection Service so you can, instead, tag all parts you want to change the colour of (easiest with this plugin), and then change the colour of the part. Your script could look something like this.

local collectionService = game:GetService("CollectionService")

local tagged = collectionService:GetTagged("ChangeColour") -- Gets all instances with the tag 'ChangeColour'

for _, v in ipairs(tagged) do
    tagged.BrickColor = BrickColor.new("Really Red")

end

Also, you can format code on the forum with three accent graves (```) and wrapping your code around it, or a single grave surrounding in-line code (e.g. print("Hello, World")