Okay, so I have a model with a bunch of neon parts and I mean a bunch like 300+ and I don't wanna change all their names bc it'll take forever so how could I change all the spotlights colors inside those block, it goes like this: Lights>Neon x300+> Spotlight(Inside every neon block), How can I change every single one of those spotlights color3 property with a switch at once?
Switching multiple light colors?
2 answers
local Lights = workspace.Lights:GetChildren()
for _,Neon in pairs(Lights) do
if (Neon:FindFirstChildOfClass("SpotLight")) then
Neon.SpotLight.Color = Color3.fromRGB(255,255,255)
end
end
6 comments
All good but in line 2, You used pairs. You are supposed to use ipairs.
That still doesn't work but no errors are output, I think it's only changing one of the many Neons inside my model, is there any way to make it change every single one of them?
I can send a screenshot of the explorer if you add me on discord if it helps any Unknown Intelligence#6569
@Trading_Opportunity Because this particular task doesn't concern the index value, you can just use the two interchangeably. You'd only really use ipairs when iterating through a dictionary which has its indices as something other than the standard ones that are automatically assigned to values.
Also, this answer has some issues. The L in SpotLight wasn't capitalized, Color3 isn't a property(Color is, Color3 is just the data type), and he put 355(which is probably just a small typo). Don't rush answers; you're prone to make mistakes and it defeats the whole purpose of an answer which is to solve an issue not create more.
Apologies;) I didn't rush this, with minimal understanding of his structural hierarchy I had to improvise, I also wrote this very late at night.
I assume your explorer looks something like this.
Insert a part into the workspace, and insert a ClickDetector inside of that part.
Create a Script inside of ServerScriptService:
local Part = game.Workspace:WaitForChild("Part")
local ClickDetector = Part:WaitForChild("ClickDetector")
ClickDetector.MouseClick:Connect(function()
for i, v in pairs(game.Workspace.Lights:GetChildren()) do
local R = math.random(255)
local G = math.random(255)
local B = math.random(255)
v.SpotLight.Color = Color3.fromRGB(R, G, B)
--Variables aren't necessary but I added them to avoid
--the assignment spanning multiple lines
end
end)
So, whenever someone clicks on the part you created, it'll loop through the entire model that contains the parts, and on each iteration, it'll change the SpotLight inside of each part so that they are a different color.
In this example, it just randomizes the color, but you can remove the three variables and the assignment of the SpotLight's color if you want to do something else.
5 comments
So I used Faehrens code and fixed a few errors and it worked, but when I tried putting it in my final copy it gave me an error: 20:34:30.283 - Workspace.Map.Alarms1.RaidAlarm.Breach.Buttons.play.Script:8: attempt to index nil with 'GetChildren' I'm not sure how to fix that, if you need to see my explorer or the full code you can shoot me a message on discord bc idk how to post screenshots here
I would consider getting a better understanding of how the explorer hierarchy works before going any further. https://developer.roblox.com/en-us/resources/studio/Explorer
Once you understand the explorer hierarchy and how it works, you should be able to tweak the code so that it fits your criteria.
Well I know how it works I just don't understand this error and why it's occuring
Okay, I fixed the code thank you for your help @RinPix, @Feahren, @darkelementallord
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
I'd recommend reading into the use of 'for loops' for changing the light's color.
I've tried using the for i,v in pairs method but it doesnt work because theyre name all the same thing :\