How to Make Server-Synced Daily Shops

Here you’re still using the same offset variable you used for days (which was in hours)

Use a separate variable for the week offset and make sure it’s in days.

Same thing for the other variables now that I look at it. Make sure you make different variables for the week calculations and day calculations, you’re using the variables for day calculations for everything

Tysm For This! Really Appreciate It!, Goodluck Making More Tutorials Like This!

1 Like

If you want the shop only to be updated weekly, you should change your wait(1) to probably wait(60) (or even higher). Checking every second isn’t needed and only uses memory, which can cause lag

2 Likes

I ended up adding a 2-second wait before running the while true do loop and then it updated correctly. Thanks for your response.

1 Like

I am trying to figure out how to apply a offset variable for the week, I want the new week to start on a monday.

local DayOffset = 4

local WeekOffset = --??


local Week = math.floor((SyncedTime.time() + WeekOffset) / (60 * 60 * 24 * 7)) -- im not sure if this is right, but it does give me the right week

Hello, how can i make it to change items every 2 hours?

1 Like

Change all the instances where it says 86400 (a day) to 7200 (2 hours)

You might have to tweak some things around if only that doesn’t work but that’s the general idea

thank you, it works the time is now at 2 hours

2 Likes

ok there is a problem it change the time but when the time get 0:00:00 it don’t change the items

1 Like

image i only change 24 to 2 and it works

1 Like

I tested your script and there’s an error at this line of code:

repeat
    shopitem = itemtablecopy[rng:NextInteger(1,#itemtablecopy)] -- ERROR
until shopitem ~= item -- until you get one that the shop table doesn't have yet

Whenever you try to run this line and get more than 5 items (6 or more than 6), it has a bug saying
ServerScriptService.ShopUpdater:101: Script timeout: exhausted allowed execution time

currentShopItems = getAvailableItems(day, 5) -- Get 5 items and supply the day as the seed, then set our currentShopItems table to the table we get from this function.

I know what the error means but how can I fix that? I tried adding a wait but when running the code, it gets stuck on “Respinning…”.

Edit: It’s weird that after it goes to 00:00:00, it will only just run the loop again without
making changes to daily shops.
image

Edit2:
Apparently it doesn’t print after the countdown ends, I’m assuming this is a bug. (Screenshot above for output)

if day ~= currentDay then -- If the day variable isn't equal to currentDay (so when the day variable changes...)
		print("c")
2 Likes

Wow nice totorial! I have a couple questions though. If you are fetching the time from a google website, would it matter if someone used a different engine, like bing, or yahoo, or a different computer like a Mac?

And,

So you are saying, if I had let’s say ten items, you would want me to only use 8 and just not use the other two(sorry if that seems like a silly question, I am still trying to understand the concepts you are using!)

1 Like

Windows or Mac doesn’t matter, on the term of a different website I’m not sure. I’m pretty sure if the other sites have a way to fetch time they’d return UTC, just like Google, so I don’t think it matters.

Yes. Think of it like this. Say you want to have 11 chosen items for the shop, but you only have 10 to choose from, and you don’t want any duplicates. Since the amount you want is more than the amount you have, it’s impossible to fill the extra slot without having a duplicate. 10 items would fill 10 slots, but you still have 1 extra one remaining. You can’t fill it without a dupe because everything has been used already

1 Like

Ok, thanks!

I am having an issue though. The time says it will reset at 5:00 PM (I used the -3 offset), and the time does actually reset like it is supposed to. The problem is the shop items don’t actually reset until the morning after, or sometime after 9:00 at night. Aren’t they supposed to be connected? Do you know off the top of your head what might be wrong or should I send code?

2 Likes

I made a post on this explaining the problem. Sadly no one has responded yet…

Welp… so much for that……Sigh :pensive:

I just tested this and the script didn’t register a new day?

image

As you can see, it didn’t update the ui (fire the remote) or print the items for today are?

i know i’m late but i’m looking for help on this problem too it doesnt update live in-game so you have to rejoin the server
Found a fix by changing + to - in local day = math.floor((syncedtime.time() - offset) / (60 * 60 * 24))

2 Likes

Hey, sorry if I missed it but, how is this script syncing with other servers?

It just keeps checking if its time to rotate the shop and since all servers are checking if its 0:00 utc then changes it