While true do loop stuck?

Hello! For some reason, my while true do loop is stuck at one if statement. Even when I set the ClockTime to 20, it keeps printing “No”

local isDay 

	while true do
		wait(1)
		if game:GetService("Lighting").ClockTime > 17 then
			isDay = true
			print("Yes")
			
	elseif game:GetService("Lighting").ClockTime < 17 then
				isDay = false 
				print("No") 
				
		end
	end
	

Thank you!

Shouldn’t it everytimes run this code:

	wait(1)
		if game:GetService("Lighting").ClockTime > 17 then
			isDay = true
			print("Yes")
			
	elseif game:GetService("Lighting").ClockTime < 17 then
				isDay = false 
				print("No") 
				
		end

What do you mean with that? Shouldn’t the while true do loop check the condition?

Perhaps consider a :GetPropertyChangedSignal or .Changed function.

1 Like

First off, you shouldn’t use a a loop to check properties in the first place. Use events, they’re much more efficient.

local timeChange = game:GetService("Lighting"):GetPropertyChangedSignal("ClockTime")

timeChange:Connect(function()
    --time changed
end)

Now, you should try changing “elseif” to just “else” in your if statement.

local timeChange = game:GetService("Lighting"):GetPropertyChangedSignal("ClockTime")

timeChange:Connect(function()
    if game:GetService("Lighting").ClockTime . 17 then
        print("yes")
    else
        print("no")
    end
end)

Try this, and see if it works.

Edit: I added an end I forgot to put in. Try the new code.

1 Like

Yep, I would use .Changed but I really want to know why the while true do loop doesn’t work. It should work fine.

Is this script on the server or client? If it is on the server and you change the time on the client, the server won’t see it.

1 Like

I tested the script and it worked fine for me, so I can only assume that you’re changing the property on the client and not the server, if you’re using this in a server script – anything done on the client will not replicate, but of course there’s a few exceptions

1 Like

Oh. That was the problem. Thank you! I completely forgot about that.