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!