I have a script that can turn on multiple lights in-game by clicking on a switch. However, I was wondering if there was an easier way to turn on the lights without needing to change each line of code when adding a new light. Here is the script:
local Light = script.Parent.Light
local Light2 = script.Parent.Light2
local Light3 = script.Parent.Light3
local Light4 = script.Parent.Light4
local Light5 = script.Parent.Light5
local Light6 = script.Parent.Light6
local Light7 = script.Parent.Light7
local Light8 = script.Parent.Light8 -- I don't want to keep repeating the same line of code each time I add a new light.
local Light9 = script.Parent.Light9
local Light10 = script.Parent.Light10
local LightSwitch = script.Parent.LightSwitch
local LightPart = Light.LightPart
local LightPart2 = Light2.LightPart2
local LightPart3 = Light3.LightPart3
local LightPart4 = Light4.LightPart4
local LightPart5 = Light5.LightPart5
local LightPart6 = Light6.LightPart6
local LightPart7 = Light7.LightPart7
local LightPart8 = Light8.LightPart8
local LightPart9 = Light9.LightPart9
local LightPart10 = Light10.LightPart10
local ClickHere = LightSwitch.ClickHere.ClickDetector
local SwitchA = LightSwitch.SwitchA
local SwitchB = LightSwitch.SwitchB
ClickHere.MouseClick:connect(function() -- Function to turn on lights
if Light.LightPart.SpotLight.Enabled == false then
Light.LightPart.SpotLight.Enabled = true
Light.LightPart.Material = Enum.Material.Neon
Light2.LightPart2.SpotLight.Enabled = true
Light2.LightPart2.Material = Enum.Material.Neon
Light3.LightPart3.SpotLight.Enabled = true
Light3.LightPart3.Material = Enum.Material.Neon
Light4.LightPart4.SpotLight.Enabled = true
Light4.LightPart4.Material = Enum.Material.Neon
Light5.LightPart5.SpotLight.Enabled = true
Light5.LightPart5.Material = Enum.Material.Neon
Light6.LightPart6.SpotLight.Enabled = true
Light6.LightPart6.Material = Enum.Material.Neon
Light7.LightPart7.SpotLight.Enabled = true
Light7.LightPart7.Material = Enum.Material.Neon
Light8.LightPart8.SpotLight.Enabled = true
Light8.LightPart8.Material = Enum.Material.Neon
Light9.LightPart9.SpotLight.Enabled = true
Light9.LightPart9.Material = Enum.Material.Neon
Light10.LightPart10.SpotLight.Enabled = true
Light10.LightPart10.Material = Enum.Material.Neon
LightSwitch.SwitchA.Transparency = 0
LightSwitch.SwitchB.Transparency = 1
else
Light.LightPart.SpotLight.Enabled = false -- Function to turn off lights
Light.LightPart.Material = Enum.Material.SmoothPlastic
Light2.LightPart2.SpotLight.Enabled = false
Light2.LightPart2.Material = Enum.Material.SmoothPlastic
Light3.LightPart3.SpotLight.Enabled = false
Light3.LightPart3.Material = Enum.Material.SmoothPlastic
Light4.LightPart4.SpotLight.Enabled = false
Light4.LightPart4.Material = Enum.Material.SmoothPlastic
Light5.LightPart5.SpotLight.Enabled = false
Light5.LightPart5.Material = Enum.Material.SmoothPlastic
Light6.LightPart6.SpotLight.Enabled = false
Light6.LightPart6.Material = Enum.Material.SmoothPlastic
Light7.LightPart7.SpotLight.Enabled = false
Light7.LightPart7.Material = Enum.Material.SmoothPlastic
Light8.LightPart8.SpotLight.Enabled = false
Light8.LightPart8.Material = Enum.Material.SmoothPlastic
Light9.LightPart9.SpotLight.Enabled = false
Light9.LightPart9.Material = Enum.Material.SmoothPlastic
Light10.LightPart10.SpotLight.Enabled = false
Light10.LightPart10.Material = Enum.Material.SmoothPlastic
LightSwitch.SwitchA.Transparency = 1 -- This is just for the switch animation.
LightSwitch.SwitchB.Transparency = 0
end
end)
Here is what it looks like in Workspace: https://prnt.sc/t7spg3

3 comments
Maybe you can use CollectionService, but I never use it before so :b https://developer.roblox.com/en-us/api-reference/class/CollectionService
Yes that can work, but this is simple enough that local Lights = theFolder:GetChildren() is enough, i suggest using CollectionService if the objects that need to be grouped in a table together are all over the place in the explorer windows, but since the Lights can just be put in a folder together, :GetChildren() should be enough
You could make a table of the lights