Night-light won't set off

I’ve been trying to make a fence light, however, the ClockTime doesn’t work. Here’s a preview of the code:

I followed the Roblox tutorial “Street Light”, it didn’t work from my previous change of this code and I tried to do this in my code.

PROPERTIES:

  • LightSwitch: A BoolValue Instance

--// Nothing is saying "Turn Off" and "Turn On" at specific time

while wait(1) do
	local ClockTime = game:GetService("Lighting").ClockTime
	local LightSwitch = script.Parent.Lights

	if ClockTime >= 17 and ClockTime <= 5 and LightSwitch.Value == false then
		warn("Turned on fence lights")
		LightSwitch.Value = true
	end
		
	if ClockTime < 17 and ClockTime > 5 and LightSwitch.Value == true then	
		warn("Turned off fence lights")
		LightSwitch.Value = false
	end
	
end

What should I change or improve?

It’s because you’re having two needed requirements, which is to have it greater or equal to 17 and less than or equal to 5 which is impossible to meet. To fix this, you want to have an OR operator instead of an AND.

while wait(1) do
	local ClockTime = game:GetService("Lighting").ClockTime
	local LightSwitch = script.Parent.Lights

	if (ClockTime >= 17 or ClockTime <= 5) then --you don't need this part > and LightSwitch.Value == false then
		if LightSwitch.Value == false then
			warn("Turned on fence lights")
			LightSwitch.Value = true
		end
	elseif (ClockTime < 17 and ClockTime > 5) then
		if LightSwitch.Value == true then
			warn("Turned off fence lights")
			LightSwitch.Value = false
		end
	end
end

EDIT: Added a check so you’re not having a warn every second.

2 Likes