Light script not working

I am trying to run every light in a folder using a master script, but I am not getting any error, but it is not working.

Here is the code.


for _, light in pairs(LightsFolder:GetChildren()) do
	
	local Light = light:WaitForChild("L")
	
	while wait(1)do
		
		if(game.Lighting.ClockTime > 6.3)and(game.Lighting.ClockTime < 17.3)then
			
			Light.PointLight.Enabled = false
			Light.Material = ("Glass")
			
		else
			
			Light.Material = ("Neon")
			Light.PointLight.Enabled = true
			
		end
		
	end

end

Any reason why it’s not working?

That is because you are stuck on a while loop on the first for loop, so the for loop doesn’t actually advance. This is why you’ll need to use spawn()

for _, light in pairs(LightsFolder:GetChildren()) do
	task.spawn(function()
	local Light = light:WaitForChild("L")
	
	while task.wait(1) do
		
		if(game.Lighting.ClockTime > 6.3)and(game.Lighting.ClockTime < 17.3)then
			
			Light.PointLight.Enabled = false
			Light.Material = ("Glass")
			
		else
			
			Light.Material = ("Neon")
			Light.PointLight.Enabled = true
			
		end

      end
  end)

end

Also please use task.wait() instead of wait()

Better yet, use the GetPropertyChangedSignal event for the ClockTime instead of a while loop.

2 Likes

Thanks for making the code as well and explaining it!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.