Countdown skipping seconds

Hi, I’m trying to make a countdown in this format: 1d 2h 3m 4s and it works great, except sometimes it skips seconds. It’s doing something like this: 27>26 | 26>25 | 25>25| 25>23, and I can’t figure out why. Any help is appreciated :slight_smile:
image

It’s likely to do with when you’re calling this function. For example, you might be calling it 1 millisecond before the seconds would roll over to the next one. Due to timing inconsistencies, fluctation could result in being called twice in the same second.

Please share the section of code where you’re scheduling calls of this GetRemainingTime function.

One easy fix for this is to use math.round to the nearest second instead of doing math.floor for seconds.

local function formatDHMS(seconds: number): string
    local days = seconds // 86400; seconds %= 86400
    local hours = seconds // 3600; seconds %= 3600
    local minutes = seconds // 60; seconds %= 60

    return string.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds)
end

Here’s the part you aksed for:


And Ziffix that code doesn’t work at all it messes up the conversion and results in something like -1d47h57m26s going up a minute or so every time

The code works just fine for me. How did you manage to get 47 hours in that formatted message anyways? 24 of those hours should have contributed to another day, making the correct output 2:23:57:26, as produced by the function I gave you. Maybe there’s an issue in how you’re calculating the expiration.

On another note, wait has been deprecated for nearly 4 years now. You’re encouraged to use task.wait instead for its improved precision. Furthermore, if you intend to generate an immediate thread with no higher-level interactions, use task.spawn over coroutine.create and coroutine.resume

You were right it actually didn’t return 47 hours but it sill gives a completely different result than expected:
image
I also replaced wait that was a mistake.
The way I calculate timeDiff is by using os.time() and substracting os.time() + a defined amount of seconds which I call Expiration and which is stored into a DataStore

Is os.time() + secondsIntoFuture a saved number, or do you always return os.time() + secondsInFuture. What is the expiration date in seconds anyway, and at what point does it skip numbers?

Expiration is a saved number that is renewed when passed, and the timer randomly sometimes just skips over numbers

Could be due to format rounding + rounding-point errors. Instead of calculating the time each second, start a numerical for loop that decrements by 1 each second