Updating a Shop for all Servers Randomly

I’m trying to have a shop in my game that, every 20 minutes, checks a table that has all the purchasable items (as strings), randomly selects 5 items, and sends the info to the client, while also updating a “valid shop items” table, so that once the client selects an item to purchase, we can check if it’s currently available to be bought. The problem is, I want the items in the shop to be the same game wide. How would I randomly select things from the table with all purchasable items, and make them the same game wide, since every server will select different things?

You best approach to a concept like this would probably incorporate some 3rd party database that stores the information about which items are available at a specific time (perhaps having some endpoint to get fetch a list for a specific time) with your roblox servers (Script inside ServerScriptService or similar position) using the HttpService to query for this “available” list of items periodically.

If you would rather keep all your code contained to purely Roblox API, then you could manually define a table of lists (each list containing the available items for that duration), then use some math to determine which list is active based on what tick() is currently returning. (This of course might cause servers to vary slightly depending on the time zone they are located in, however you may be able to get around this by making accommodations for the time zone differences)

1 Like

I have a few ideas:

Idea #1: math.random can generate “random” numbers, but can get the same sequence of those “random” numbers by setting the same seed. The seed is an initial value passed to the algorithm that generates a random number and that same random number is used as the seed for the subsequent call to the math.random function. With this in mind, you can generate a seed that is subject to change every 20 minutes, something like this:

-- finding seed
local start = 3000
local multiplier = 1.578e3
local seed = start + math.floor(os.time() / (20 * 60)) * multiplier

-- getting random items

for i = 1, #items do
    print( math.random(1, #items) )

start and multiplier can be changed to your liking so that the seed can be spread out every 20 minutes. You can change the equation however you want but this should work fine.

Idea #2: You can establish a web server that will randomly pick items every 20 minutes and set up a REST API endpoint that the game servers can communicate with. You can handle the server with Node.js and look for hosting services. If you’re looking for free hosting, you can use Glitch.

1 Like

Lets stay away from hosting your own webserver to do something this simple.

Since all servers can return the same time since EPOCH in UTC+0 using a DateTIme object you can just use that as a seed. Construct a Random object with the current time–Random.new(Seed). The Random object who will generate the same number on the n-th call as in every other server.

You could also set the seed using math.randomseed(Seed) though in my opinion it is better to use the Random object so you can have different seeds for different things.

Now all you have to do is generate some numbers using your Random object and it will generate the same sequence of random numbers in each server.

local Randomizer = Random.new(Seed)
local Items = { ... }

local Shop = {}
local ShopSize = 10

for i = 1, ShopSize do
    table.insert(Shop, Items[Randomizer:NextInteger(1, ShopSize)])

The servers should never be out of sync except for when a new server is created–though this should be easily fixed by using messaging service to check if all servers are synced–though I don’t think you will have any issues rounding the current TimeStamp to the closest 20th minute.