Checking if time is in a certain range?

I didnt really know how to word the title, so just hear me out.

I have a time script that sets a “Night” BoolValue to true and false:

    if Lighting.Old.Hour.Value == 6 and Lighting.Old.Minute.Value == 0 or Lighting.Old.Hour.Value == 21 and Lighting.Old.Minute.Value == 0 then
		game.Lighting.Night.Value = true
	end
	if Lighting.Old.Hour.Value == 6 and Lighting.Old.Minute.Value == 0 then
		game.Lighting.Night.Value = false
	end

And this script works, but only if the time hits the actual goal, which is 21 and 6
If the time is 11 and I set the time to 4, the Night value will not get changed, as it didnt hit 6 or 21

How could I solve this?

EDIT: Also you cant really do
if hour >= 21 and hour.Value < 6 then night = true end
Because it cancels itself out

I would recommend using a .Changed event, and, can you show the script that changes the hour value?

I do use the .Changed event every time the minute value is changed.
The basics of the hour script is that it adds 1 to the Minute value every second, if the Minute value is 60 then set Minutes to 0 and add 1 to Hours.

I’m pretty sure you were supposed to use an or there instead of and.

2 Likes

Shouldn’t something like this work?

if time > 10 or time < 12 then
    — code
end
1 Like

I was going to say that this does not work correctly with this coding:

    if Lighting.Old.Hour.Value >= 21 or Lighting.Old.Hour.Value < 6 then
		game.Lighting.Night.Value = true
		print("night")
	end
	if Lighting.Old.Hour.Value >= 6 or Lighting.Old.Hour.Value < 21 then
		game.Lighting.Night.Value = false
		print("day")
	end

But then I realised I have to do an else instead of a new if statement:

    if Lighting.Old.Hour.Value >= 21 or Lighting.Old.Hour.Value < 6 then
		game.Lighting.Night.Value = true
		print("night")
	else
		game.Lighting.Night.Value = false
		print("day")
	end

It works nicely. Thank you!