How do I fix this script offsync in my day cycle

Alright so I’m still making a pretty simple day cycle, but I realized pretty quickly that even though the math is perfect, my 1 minute day timer is anywhere from 1-4 seconds over. This is from what I understand, because roblox doesn’t fire code instantaneously. But I was wondering if there is anything I can do to combat this?

local MinutesPerDay = 1  -- Duration of a full day cycle in minutes
local UpdateInterval = 0.1  -- Time in seconds to wait before updating ClockTime

-- Calculate total seconds in the day cycle
local TotalSecondsInDay = MinutesPerDay * 60  

-- Calculate total updates
local TotalUpdates = TotalSecondsInDay / UpdateInterval  

-- Calculate ClockTime increment per update
local ClockTimeIncrement = 24 / TotalUpdates

So this is essentially an auto conversion script, it turns the 1 minute into 60 seconds, then divides the seconds by how fast I want it to update, and then divides 24(a full day) by that to get how many times I would need to update the time and by how much.

So for 1 minute, I’d need to update the timer 600 times by 0.04 to reach 24.

Now the problem I’m having, is since roblox doesn’t fire script instantly, each one of these 600 times it needs to fire is adding a slight delay, causing up to 4 seconds of total delay, so my day isn’t 60 seconds, but from 60-64 seconds. Normally this wouldn’t be a problem but it’s going to get worse when my days get longer.

1 Like

I’ve looked into deltatime and so far I haven’t been able to figure anything out.

I think your best bet would be to just find the total time of the day, including the extra time, and then converting that to a decimal, (24 hours / 24 hours 4 seconds), and then multiplying that to TotalSecondsInDay to find a really close estimate of the actual time of day.
(24 hours/25 hours) = .96
.96*25 = 24.
So using that formula, you can figure out how much less you should wait in your interval. it should be like .995 or something like that.

I am currently unable to provide any code to how this would work, but here’s an idea. Have a while true do loop that updates every x seconds that sets the time to the answer of an equation that’s something like time() * (math and stuff).
Basically, you’d be able to say something like: when time() (seconds server has been up) is 30 seconds, then it’s 12 PM (assuming 1 minute per 24 hours in game)
Maybe someone who’s picking up what I’m saying can explain it better

This would not work because the offset is different each time.

Well yeah thats how the script works, but that doesnt address the issue im having

It would because your current code determines how many update intervals and how fast. Updating clock time with an equation that could determine the time based on how many seconds the server has been up would not delay

EDIT: I’m kind of overcomplicating it. A simpler yet less effective solution would be to wait until the ClockTime is between like 0-0.5 and then “reset” the time and the offset
If this doesn’t work, then use something like this

local DayVariable = 1 --each day is how many minutes?

while true do
task.wait(0.2) --how fast you want it to update
game.Lighting.ClockTime = 0.4*(1/DayVariable)*time()
end

Something like this should work I think. Sorry for bad format and terrible variable name lol.
Hope this helps

That won’t work for my game and is pretty situational, the solution I’m currently working on and the solution that should work is;

After the delay, you can use os.Clock() to find the Deciseconds and subtract them, so the next second will only be delayed by slightly anything, and in another second that is negated anyway.

Basically, remove the old delay instead of trying to prevent new delay, this way it doesn’t add up and stays manageable and negligible.

image
The first day cycle was started while the game was still loaded, the other two are BARELY off, less than 0.01 seconds.

local offset = os.clock() / UpdateInterval % 1 * UpdateInterval -- Calculates the old delay/Offset in ms
task.wait(UpdateInterval - offset)  -- Removes the offset from the total wait.
1 Like