Not all the Lights are turning on?

Hello!

I’m making a lamp that turns On and off when the player press X by adding them into tables and then Enabling them but the problem is not all of them are turned on that’s very weird why this is happening?

I tried printing the table and all the lights are in the table so what is the problem?

Script:

elseif input.KeyCode == Enum.KeyCode.X then

	OnOff = not OnOff

	local Lamp = game.Workspace:WaitForChild("Map")["Lamp"]

	for i,v in ipairs(Lamp["LightPart"]:GetChildren()) do

		if v:IsA("PointLight") then
			table.insert(LightsPoint,v)
		end
		if v:IsA("SurfaceLight") then
			table.insert(LightsSurface,v)
		end
	end
	print(LightsPoint)
	print(LightsSurface)
	if OnOff then
		
		for i,v in pairs(LightsSurface) do
			if Lamp["LightPart"]:FindFirstChild(v.Name) then
				LightEnableSur = v
			end
		end
		
		for i,v in pairs(LightsPoint) do
			if Lamp["LightPart"]:FindFirstChild(v.Name) then
				LightEnablePoint = v
			end
		end
		
		LightEnablePoint.Enabled = true
		LightEnableSur.Enabled = true
		wait()
		table.clear(LightsPoint)
		table.clear(LightsSurface)
	else
		LightEnablePoint.Enabled = false
		LightEnableSur.Enabled = false
		wait()
		table.clear(LightsPoint)
		table.clear(LightsSurface)
	end
	--print(OnOff)
end

When the loop finishes, only a single light surface and point is stored in the variables, therefore only the last v of each loop gets enabled. Instead remove those variables and set the .Enabled property directly inside the loop:

for i,v in pairs(LightsSurface) do
	if Lamp["LightPart"]:FindFirstChild(v.Name) then
		v.Enabled = true
	end
end
		
for i,v in pairs(LightsPoint) do
	if Lamp["LightPart"]:FindFirstChild(v.Name) then
		v.Enabled = true
	end
end
1 Like