Hi Guys, so I was seeing a code available on toolbox for Day-Night cycle, just for my own learning, but I’m stupid enough to not understand what does the code really do.
local dayLength = 12
local cycleTime = dayLength*60
local minutesInADay = 24*60
local lighting = game:GetService("Lighting")
local startTime = tick() - (lighting:GetMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime
local timeRatio = minutesInADay / cycleTime
if dayLength == 0 then
dayLength = 1
end
repeat
local currentTime = tick()
if currentTime > endTime then
startTime = endTime
endTime = startTime + cycleTime
end
lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
wait(1/15)
until false
Also, on the top of the script, it was written that DayLength is the length of the day in minutes. But looking at it seems that it means hours. And there are some more questions like this in my mind. Anybody explaining this code would be alot appreciated!
Thanks!
dayLength : Sets the length of a day in hours (default 12). cycleTime : Calculates the total time in seconds for a day-night cycle based on dayLength . minutesInADay : Represents the total number of minutes in a real-world day (1440).
lighting : References the Lighting service in the game. startTime : Calculates the start time of the current cycle based on the current time in the game. endTime : Calculates the end time of the current cycle. timeRatio : Determines the conversion factor between game time and real-world minutes.
The repeat...until false loop creates an infinite loop to continuously update the lighting
currentTime : Gets the current time in seconds since the game started.
Checks if the current time exceeds the end time of the cycle. If so, resets the start and end times for the next cycle.
Calculates the current time within the cycle using timeRatio and sets the Lighting.MinutesAfterMidnight property accordingly.
Waits for 1/15th of a second before the next iteration.