Automatic Lights Day/Night Cycle Not Working

I’m trying to make an automatic day/night cycle with these torches so that at day time the torches are off but at night they are on.

The issue is that the script isn’t working at all and I have no idea what I did wrong.

I looked at tutorials that are similar to what I’m doing but they didn’t help me solve the problem.





local lighting = game:GetService("Lighting")

function Lights(boolvalue)
	if boolvalue == true then
		
		for i, v in pairs(script.Parent:GetDescendants()) do -- night
			if v:IsA("PointLight") or v:IsA("Smoke") or v:IsA("ParticleEmitter") then
				v.Enabled = true
				
			elseif boolvalue == false then
				
				for i, v in pairs(script.Parent:GetDescendants()) do -- day
					if v:IsA("PointLight") or v:IsA("Smoke") or v:IsA("ParticleEmitter") then
					v.Enabled = false
					end
				end
			end
		end
	end
end

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if lighting.ClockTime >= 18.5 or lighting.ClockTime <= 7 then -- night time
		Lights(true)
	elseif lighting.ClockTime >= 7 or lighting.ClockTime <= 18.5 then -- day time
		Lights(false)
	end
end)

I think this is your issue. Nothing inside the lights function runs unless the boolvalue is true, and here you’re checking if the boolvalue is false. The script never reaches this point if the boolvalue is false, since it gets stopped here:

1 Like

Try replacing your function with this:

function Lights(boolvalue)
	if boolvalue == true then
		
		for i, v in pairs(script.Parent:GetDescendants()) do -- night
			if v:IsA("PointLight") or v:IsA("Smoke") or v:IsA("ParticleEmitter") then
				v.Enabled = true
			end
		end
	elseif boolvalue == false then

		for i, v in pairs(script.Parent:GetDescendants()) do -- day
			if v:IsA("PointLight") or v:IsA("Smoke") or v:IsA("ParticleEmitter") then
			v.Enabled = false
		end
	end
end
3 Likes

It gave an error at first however I just added another end and it worked thank you! :smiley:

1 Like