Why is my script not working?

I’m trying to get a lamppost to turn on when the ClockTime is above 17 or below 6.4. They will turn off if it is not between those times, but they will not turn on. Can someone help me with this?

Script:

local Time = game.Lighting.ClockTime

while true do
	if Time < 6.4 or Time > 17 then
		script.Parent.Lamp.PointLight.Enabled = true
		script.Parent.Lamp.light.Enabled = true
		script.Parent.Part.PointLight.Enabled = true
	else
		script.Parent.Lamp.PointLight.Enabled = false
		script.Parent.Lamp.light.Enabled = false
		script.Parent.Part.PointLight.Enabled = false
	end
	wait(1)
end

Model:
afbeelding_2022-01-10_192421

1 Like

You’re only setting Time once, when the game starts.

Try moving that first line to inside the loop.

Also, just an unsolicited suggestion that you can listen specifically for changes to the clocktime and only update things if it changes.

local function Update()
	local Time = game.Lighting.ClockTime
	if Time < 6.4 or Time > 17 then
		script.Parent.Lamp.PointLight.Enabled = true
		script.Parent.Lamp.light.Enabled = true
		script.Parent.Part.PointLight.Enabled = true
	else
		script.Parent.Lamp.PointLight.Enabled = false
		script.Parent.Lamp.light.Enabled = false
		script.Parent.Part.PointLight.Enabled = false
	end
end

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(Update)

Update()
1 Like

Thank you for your help, it works!