While wait(1) do Loop not functioning as intended

Hello there!

I got a neon sign that i want to have come on and turn off at 2 times of the day. 6am and 6pm. However for some reason my script is just looping it none stop. and Idk what to do.

I am totally lost. For some reason when i put this script on a while wait(1) do loop it just constantly loops the Day() and Night() function i’ve made for them.

I feel its looping because they aren’t just switching on and off, but playing a sequence (almost like an animation) but idk how to make it play it once on the 2 respected times (6am and 6pm) without looping.

the On is a bool value that is being used to check if the lights are on or off. If i need to get rid of it, lmk :slight_smile:

Any ideas, advice or results is highly appreciated :slight_smile:

script:

while wait(1) do

    print("Checking Time")

    if game.Lighting:GetMinutesAfterMidnight() >= DayTime * 60 then
	    On.Value = true
	
	    if On.Value == true then
		    On.Value = false
		    print("Beginning Day Sign")
		    Day()
		
	    end
    end

    if game.Lighting:GetMinutesAfterMidnight() >= NightTime * 60 then
	    On.Value = false
	
	    if On.Value == false then
		    On.Value = true
		    print("Beginning Night Sign")
		    Night()

		end
	end
end

link to the video showing a visual aid:

1 Like

Maybe instead of 2 if statements you could do else if?

if game.Lighting:GetMinutesAfterMidnight() >= Daytime * 60 then
     On.Value = true

if On.Value == true then
     On.Value = false
          print("Beginning Day Sign")
      Day()
elseif Game.Lighting:GetMinutesAfterMidnight() >= NightTime * 60 then
   On.Value = false

If On.Value = false then
     On.Value = true
      print("Beginning Night Sign")
   Night()
-- I don't know how many ends to put just put how many it wants
1 Like

Hey! Thank you for the advice, sadly it still loops, and now its just looping Day() endlessly regardless of the time :frowning:

Oh oops might be because I spelt DayTime wrong

Unfortunately no, I did fix the errors that showed up and added the appropriate amount of end(s).

Maybe instead of wait try heartbeat? Im not too sure either

https://developer.roblox.com/en-us/api-reference/event/RunService/Heartbeat

1 Like

I’m not sure how you’ll fix it, but its not working because when the time > night it will also be true that time > day and both if statements will be true so they are fighting with each other.

Maybe add an AND check like:
if time > daytime and time < nighttime then

if time > nighttime then…

I think a big reason is that in each if-block, you are setting On.Value to something immediately before testing its value. So, the if-check will always be true and the code will execute.

Try initializing On.Value before going into the while-loop instead. I initialized it to false due to the first if-check in the revised version.

Also, to prevent calling Lighting:GetMinutesAfterMidnight() an extra time, save it in a variable.

It’s also recommended to avoid doing the “while wait() do” thing, so I changed it to “while true do” and moved wait(1) to its own line.

Lastly, I changed the if-conditions to check if it’s night first, and only if it isn’t then check if it’s day. That way, they won’t both be true and execute both blocks.
You could also try using the new task.wait(1) instead.

On.Value = false
while true do
  wait(1)
  print("Checking Time")

  local minutesAfterMidnight = game.Lighting:GetMinutesAfterMidnight()

  if minutesAfterMidnight >= NightTime * 60 then
    if On.Value == false then
      On.Value = true
      print("Beginning Night Sign")
      Night()
    end
  elseif minutesAfterMidnight >= DayTime * 60 then
    if On.Value == true then
      On.Value = false
      print("Beginning Day Sign")
      Day()
    end
  end

end

I’m not at my home PC so i can’t test this but I hope it helps you.

2 Likes

Instead of making it constantly loop, you could instead have it go upon the property “ClockTime” changing.

This way, instead of having to make it continually loop through all the options then shut itself off and cause all sorts of weird issues; it will just activate once it is found fit on the “ClockTime” changing.

local lighting = game.Lighting

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
-- do your code here 
end

(i am unsure if this will solve your issue, but it is worth a shot

Adding on to @Messeras here is a working example (or at least it should be) that I have tested.

local Lighting = game:GetService("Lighting")

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	local Hours = math.floor(Lighting.ClockTime)

	if Hours == 6 then
		print("6am")
	elseif Hours == 18 then
		print("6pm")
	end
end)
1 Like

Sorry for such a delayed responce! Yes this works! Thank you!

2 Likes