Daily shop rotation is not rotating at intended time?

Continuing the discussion from How to make a Daily Shop that updates through all servers?:

Hey Dev Forum

I’ve been repurposing the code I found from the discussion linked to this post and have come across a weird problem with when the shop updates.


The timer is accurate to when I want it to update but the shop items do not update at the correct time.


I think it is something to do with the day and currentDay variables not changing even when the timer reaches 00:00:00, and I’m not sure how to fix it.

local currentDay = nil
local currentShopItems = {}
local offset =  (60 * 60 * 17) -- Os.time() / this synced time module both return proper Unix time, which is started from 12:00 AM on Thursday, January 1, 1970. I offset it by 17 hours for it to become 5 PM PST.
-- 1736467200
task.spawn(function()
	while task.wait(1) do
		local day = math.floor((syncedtime.time() + offset) / (60 * 60 * 24))
		local t = (math.floor(syncedtime.time())) + offset -- Sets the date to Thursday 5PM PST
		local origtime = t - offset
		local daypass = origtime % 86400
		local timeleft = 86400 - daypass
		local timeleftstring = toHMS(timeleft)
		
		repStorage.Storage.TimeUntilShopReset.Value = timeleftstring

		--print("The time left until 5:00 PM PST is ".. timeleftstring) -- Optional printout, obviously I'd use "timeleftstring" for a countdown timer in a shop
		--print(day, math.floor((syncedtime.time() + offset + (timeleft - 10)) / (60 * 60 * 24)))
		if day ~= currentDay then
			currentDay = day
			currentShopItems = getAvailableItems(day)
			print("The items for today are these: a ".. table.concat(currentShopItems, ", "))
			print('Updated shop items')
			-- rest of updataing logic cut out for the sake of readability

		end
		
	end

end)

Thanks!

Hello, I believe the issue lies in your calculations here. When calculating the day, you are offsetting the time by an offset; In this case being 17 hours. This means that the day count will increase at 5:00 PM (17:00) UTC instead of 12:00 AM (00:00). When you calculate the time left for the day to change, you are removing the offset, therefore assuming that the day changes at 12:00 AM UTC instead of 5:00 PM.

To fix this, you can either remove the offset from calculations entirely or replace origtime with t when calculating daypass. Hope this helps!

I understand what you mean! I’ve implemented the fixes, and now I gotta wait a few hours to see what happens. Thanks for responding!

It worked!!! Thank you so much for helping!