If statement only works through first loop?

I’m still new to Lua, but I understand the concept of how some concepts work.

  1. What do you want to achieve? I am attempting to create a window, in which when the game time is night, it glows; and when it is daytime the window doesn’t glow.

  2. What is the issue? The issue is I start the game at night. When I set it to mid-day, the window does not turn back to non-neon. Vice-versa occurs when it starts from the day and I set it to night, where it doesn’t turn neon.

  3. What solutions have you tried so far? I have tried playing with the time a bit, but I am not advanced at Lua; so I don’t know what else to change.

The “Time Check” does appear in the output log every 1 - 10 seconds, which is telling me the script is indeed going through.

local chance = math.random(1,2)

while true do
	wait (math.random(1,10))
	print "Time Check"
	if game.Lighting:GetMinutesAfterMidnight() == 6 * 60 then
		script.Parent.BrickColor = BrickColor.Black()
		script.Parent.Material = Enum.Material.Glass
	end
	
	if game.Lighting:GetMinutesAfterMidnight() > 18 * 60
	and chance >= 1.5
	then
		script.Parent.BrickColor = BrickColor.new("New Yeller")
		script.Parent.Material = Enum.Material.Neon
	end
end

I believe it could be something to do with the while true do not including the if statements after the first loop?

You’re doing an exact comparison here when the time could be anything the moment your script gets to here. wait(n) waits for at least n, but is not guaranteed to be exact. So if you rely on the game time being updated somewhere else, this probably won’t match up a lot.

Also I don’t know the context of this code, but chance will only be set once in this window of code. So if it’s < 1.5, the loop will never enter that condition.

1 Like

The == you have pointed out has fixed the problem, thank you.

Also, chance was purposely only set once. It decides if the window will light up when it is night time.

Thank you for the assistance!