I have a working day night cycle but I want to detect when it becomes night/day, problem is when I print(Night) or print(Day) it prints a bunch. I tried adding a debounce but it just breaks the coroutine. How do I deal with this? Should I be using repeat? This is a server script.
local Thread = coroutine.wrap(function(...)
repeat
local currentTime = tick()
if currentTime > endTime then
startTime = endTime
endTime = startTime + cycleTime
end
Lighting:SetMinutesAfterMidnight((currentTime - startTime)*timeRatio)
if Lighting.ClockTime >= 17.6 or Lighting.ClockTime <= 6.3 then
print("Night")
elseif Lighting.ClockTime <= 17.6 or Lighting.Clocktime >= 6.3 then
print("Day")
end
wait()
until false
end)
If it’s running every frame and it is printing a bunch of times, then yes it’s working as intended because in your code, anywhere within 17.6 to 6.3(and vice versa) will activate the print statement.
So in order to print once, just check if
if Lighting.ClockTime == 17.6 then
print("Night")
end
But this will continue printing given that the time is still equal to 17.6
so just throw in a variable like isNight
local isNight = false;
if Lighting.ClockTime == 17.6 and isNight == false then
print("Night")
isNight = true; -- for day time
end
if Lighting.ClockTime == 6.3 and isNight == true then
print("Night")
isNight = false; -- for night time
end