How to make variables same values across all servers

What I am trying to make is a shop with items that have limited stocks, meaning that when someone buys it, the stock goes down, so only a number of people can buy it (Like limiteds from roblox). I’m thinking about MemoryStoreService, but I don’t really know how to use it well, as there are really no tutorials except the documentation.

Example: Player1 buys [item], Stock decreases from 30 to 29, Shows in all servers as 29 now, until someone buys it again, then showing 28, until 0, where no one can buy it and it’s out of stock.

1 Like

MessagingService.
and DataStore

I need a breakdown on what to do, like I want to make the stockamounts random, but be the same on all servers, so it goes down in sync, but im not sure what to do because when i start the server, if there are no servers then it should get the stored data.

on the server, if there no value on data store then do math.random() and save value to data store.
else if there is value, then get it

code: edit to your needs

local DataStoreService = game:GetService("DataStoreService")
local limitedValueStore = DataStoreService:GetDataStore("item1")

local value

local success, err = pcall(function()
	limValue = limitedValueStore:GetAsync(limitedValue)
end)

if success then
    if limValue == nil then
        local success, err = pcall(function()
		     limitedValueStore:SetAsync(limitedValue, math.random(5,30) - 1)
	    end)

	    if success then
		   -- your code here --
	    else
		    warn(err)
        end
	elseif limValue > 0 then
        local success, err = pcall(function()
		     limitedValueStore:SetAsync(limitedValue, limValue - 1)
	    end)

	    if success then
		   -- your code here --
	    else
		    warn(err)
        end
    end
else
	warn(err)
end
1 Like