Hourly Reset Timer that is global

Basically, I have this system where every hour, where the merchant restocks every hour (e.g. 12:00, 13:00).

I want to display a timer on the merchant that counts down to 0, and resets back to 1 hour after hitting it.

Now, this’d be easy to do, but I want it to sync in every server possible, and in this department, I have no clue how to do that.

Any help is appreciated, thanks.

You can use the os.date() function.

local current = os.date("*t")

local hour = current.hour
local mins = current.min
local seconds = current.sec

--convert minutes to seconds
local rawMins = mins * 50
--add the seconds to this
rawMins += seconds

--now, run a count controlled iteration until the hour is reached
for i = rawMins, 3600, 1 do
    local current = 3600 - i --get the current countdown position

    --convert the current counter to minutes and seconds
    local mins = math.floor(current / 60)
    local secs = math.floor(current % 60)

    --do things with these timer values
end

You can use DateTime to get a timestamp consistent across all servers. Then, you can restock every hour on the hour by getting the previous/next restock timestamps based on the current time.

local now = DateTime.now().UnixTimestamp
local frequency = 60*60 -- restock every hour

local lastRestock = now - (now % frequency)
local nextRestock = lastRestock + frequency

Then you can have a timer to restock the merchant once DateTime.now().UnixTimestamp > nextRestock. If the merchant has random odds, you can also seed the generator with the lastRestock, so the items will also be identical across servers.

For some reason the time is unsynced.

Shouldn’t it be 15 minutes, not 22?

Sorry if it’s a dumb question but I’m genuinely confused.

Nvm, I fixed it, ty for the script! :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.