I'm working on an artificial stock market and it works by saving values with DataStoreService and loading them onto a server, and once loaded it will randomly increase or decrease the value of the stock. Of course, this means that each server will be able to increase or decrease the stock individually, and I want them to be the same across all servers.

I thought of a way to fix this by making one server (the server which is first loaded when someone joins the game) the server which actually increases/ decreases the stock value, and all other servers simply set their stock value to what the original server had generated using DataStoreService.

I was going to do this by seeing when the first server of the game is created, and setting a bool value to false. If the bool is false, my stock value script will not generate its own values and check for the DataStoreService instead. If the bool is true, it'll make a random value and send it to the DataStoreService.

However, the problem I have is that I'm not sure how I would assign the value to only the first server, and subsequently how to stop all other servers after that from trying to set the "first server" boolean to true.

Below is the code I use to save my data:

local DSS = game:GetService("DataStoreService")
	local datastore = DSS:GetDataStore("DatasSaver")

	function MakeKey(player)
		local ret = "key_" .. player.UserId
		return ret
	end
	 
	function Table(player)
		local dataTable = {oil = game.ServerScriptService.Stocks.Oil.Value}
		return dataTable
	end
	
	function SaveData(player)
		local key = MakeKey(player)
		local data = Table(player)
		datastore:SetAsync(key,data)
	end
	
	function GiveValue(player, data)
		game.ServerScriptService.Stocks.Oil.Value = data.oil
	end
	
	function LoadData(player)
		local key = MakeKey(player)
		local data = datastore:GetAsync(key)
		GiveValue(player, data)
	end
	
	game.Players.PlayerAdded:Connect(function(player)
	local players = game.Players:GetPlayers()
	if  #players == 1 then
    LoadData(player) 
  end
end)

game.Players.PlayerRemoving:Connect(SaveData) 

Here's the script I use for generating a new stock value, if that's of any use:

subadd = {"add", "sub"}
wait(1)
oil = game.ServerScriptService.Stocks.Oil
lastprice = oil.Value

while true do
	print("Oil: " .. oil.Value .. ", Last Price: " .. lastprice)
	lastprice = oil.Value
	wait(10)
	choice = subadd[math.random(1,#subadd)]
	numberchoice = math.random(80)
	
	if choice == "add" then
	oil.Value = oil.Value + numberchoice
	else
	oil.Value = oil.Value - numberchoice
	end
end

I believe I saw an article from Roblox's API reference saying I could use HttpService to do something like this, but I can't find any good sources about how to extract data from a website and put it into my Roblox game (I've only used the service for Discord related things and with that you only need to send data) If anyone has some information on that, that would also be greatly appreciated.

1 comment

1

There is now MessageService, which is a pub sub and probably better suited for the actual messaging. If you don't want to run your own webserver, you still might need to use Datastores to set and validate which server instance is the authority.