How do I put 1 variable into multiple part directories

Hey ya’ll, I am trying to make all the lights turn on at the same time but without having to type them all out individually which takes forever and makes a long script. The script does work, but how do I make it all in 1 variable so I don’t have to type much?

local state = false
local Light1 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B.PointLight
local Light2 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B1.PointLight
local Light3 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B2.PointLight
local Light4 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B3.PointLight
local Light5 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B4.PointLight
local Light6 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B5.PointLight
local Light7 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B6.PointLight
local Light8 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B7.PointLight
local Light9 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B8.PointLight
local Light10 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B9.PointLight
local Light11 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B10.PointLight
local Light12 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B11.PointLight
local Light13 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B12.PointLight
local Light14 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B13.PointLight
local Light15 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B14.PointLight
local Light16 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B15.PointLight
local Light17 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B16.PointLight
local Light18 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B17.PointLight
local Light19 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B18.PointLight
local Light20 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B19.PointLight
local Light21 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B20.PointLight
local Light22 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B21.PointLight
local Light36 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B35.PointLight
local Light37 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B36.PointLight
local Light38 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B37.PointLight
local Light39 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B38.PointLight
local Light40 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B39.PointLight
local Light41 = script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5.B40.PointLight


function Lights()
	state = not state
	if state then
		Light1.Enabled = true
		Light2.Enabled = true
		Light3.Enabled = true
		Light4.Enabled = true
		Light5.Enabled = true
		Light6.Enabled = true
		Light7.Enabled = true
		Light8.Enabled = true
		Light9.Enabled = true
		Light10.Enabled = true
		Light11.Enabled = true
		Light12.Enabled = true
		Light13.Enabled = true
		Light14.Enabled = true
		Light15.Enabled = true
		Light16.Enabled = true
		Light17.Enabled = true
		Light18.Enabled = true
		Light19.Enabled = true
		Light20.Enabled = true
		Light21.Enabled = true
		Light22.Enabled = true
		Light36.Enabled = true
		Light37.Enabled = true
		Light38.Enabled = true
		Light39.Enabled = true
		Light40.Enabled = true
		Light41.Enabled = true
		script.Parent.Parent.Background.BackgroundColor3 = Color3.new(1, 0, 0)
		wait()
	else
		Light1.Enabled = false
		Light2.Enabled = false
		Light3.Enabled = false
		Light4.Enabled = false
		Light5.Enabled = false
		Light6.Enabled = false
		Light7.Enabled = false
		Light8.Enabled = false
		Light9.Enabled = false
		Light10.Enabled = false
		Light11.Enabled = false
		Light12.Enabled = false
		Light13.Enabled = false
		Light14.Enabled = false
		Light15.Enabled = false
		Light16.Enabled = false
		Light17.Enabled = false
		Light18.Enabled = false
		Light19.Enabled = false
		Light20.Enabled = false
		Light21.Enabled = false
		Light22.Enabled = false
		Light36.Enabled = false
		Light37.Enabled = false
		Light38.Enabled = false
		Light39.Enabled = false
		Light40.Enabled = false
		Light41.Enabled = false
		script.Parent.Parent.Background.BackgroundColor3 = Color3.new(1, 1, 1)
		wait()
	end
end
				
script.Parent.MouseButton1Click:Connect(Lights)
1 Like

For a quick response I think you have to use something with GetChildren() which gets all the Instances that are directly parented to the Instance you use GetChildren() on.

https://create.roblox.com/docs/reference/engine/classes/Instance#GetChildren

I suggest using for loops instead, that is if all of the lights are in a folder or model, and that they are identical to each other

Yeah maybe a for loop with GetChildren?

1 Like

Like this for the function lights on the part, you can write something like this:

for i,v in pairs(folder:GetChildren()) do
v.Enabled = true
end

or if you want to turn in off just do the same thing but turn enabled = false

for i,v in pairs(folder:GetChildren()) do
v.Enabled = false
end

With all the knowledge you have me, this might work:

local state = false

local function Lights()
	state = not state
	
	if state then
		for _, v in pairs(script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5:GetChildren()) do
			v.PointLight.Enabled = true
		end
		script.Parent.Parent.Background.BackgroundColor3 = Color3.new(1, 0, 0)
		wait()
	else
		for _, v in pairs(script.Parent.Parent.Parent.Parent.Parent.Parent.LightControl.LUTN.Lightscheme5:GetChildren()) do
			v.PointLight.Enabled = false
		end
		script.Parent.Parent.Background.BackgroundColor3 = Color3.new(1, 1, 1)
		wait()
	end
end

script.Parent.MouseButton1Click:Connect(Lights)

Obviously you may be able to possibly shorten this by using game.Workspace and descending from there, but like I said, I’m using what I have gotten from the code you provided. And also, your code has been shortened by 78 lines!

1 Like

@Shamu_Flyer Now that I think about it, this type of problem should be placed in #help-and-feedback:code-review. Just a reminder for next time.

I didn’t know that category existed, unless it has been all this time but I didn’t notice it at all. I’ll try that next time!