How to Make Server-Synced Daily Shops

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

That’s not related to the question.

Hope this explains it to you :).

2 Likes

Okay I’m back after a few days of making my own daily synced shop. This is such a goldmine of a resource, thank you for creating it. Without it I probably would’ve tried to use DataStoreService or some other horrible technique.

bf78ed599cefa01b9a6712f59716cf53

Super cool (as well as synced & random) results. My store is a little bit more complex than the tutorial, but I managed to figure it all out. Random.New(seed) is by far my most favorite thing about Roblox, even though it’s quite basic. :smiling_face_with_three_hearts:

1 Like

Hey there! Going to post here again because I have a question. The tutorial is super good, so good that I am using for my daily objectives update. My only issue right now is trying to figure out how to reset data without doing it every time the script loads.

When you join the game and this starts up,

while true do
	local day = math.floor((syncedtime.time() - offset) / (60 * 60 * 24))
	local t = (math.floor(syncedtime.time())) - offset
	local daypass = t % 86400
	local timeleft = 86400 - daypass
	local timeleftstring = toHMS(timeleft)
	
	local sentence = ("Time till tasks reset: " .. timeleftstring)
	
	if day ~= currentDay then
		currentDay = day
		currentBasicTasks = getAvailableItems(day, 3)
		print("Today's Basic Tasks: " .. table.concat(currentBasicTasks, ", "))
		currentPremiumTasks = getAvailablePremiumItems(day, 3)
		print("Today's Premium Tasks: " .. table.concat(currentPremiumTasks, ", "))
		game.ServerStorage.NameChangeEvent:Fire()
	end
	updateTimerEvent:FireAllClients(sentence)
	wait(1)
end

every time that if statement executes, because the day is never = to the currentDay when the script starts, even when technically it is. How can I fire this statement

game.ServerStorage.NameChangeEvent:Fire()

but only when it needs to happen(when 24 hours are up) instead of every time the script loads
If I am not explaining myself well please tell me, I would like to figure this out!

Really nice tutorial! I mainly used it for a reference to see if my approach matched up with how other people tackle the problem.

Thank you this helped me a lot and save me a lot of time :+1:

umm you can make a remote event which every time the shop resets it fires all clients to refresh the shop

Hey that is great but i have a question is that for single server or for globally? Like the stuffs in shop has same on all servers

It’s the same on all servers. Random.new(day) makes sure that all servers will get the same RNG, meaning everything will generate the same on every server.

This actually helped alot Most of the time’s these things are really difficult to make thanks so much!