Keeping track of trade data?

So im currently working on a trade system and was wondering what the best way is to keep the trade data for the players im currently using a server sided dictionary with the key being one of the 2 players user ids but I just feel like these is a more efficient way of doing it.

2 Likes

I have one question, why are you only going to store one of the players userid’s? It would be easier if you stored both. Here is a little example of what I am talking about:

local Trades = {
	[1] = {  -- Trade Number
		["Player1_UserId"] = {}; -- Pets go here
		["Player2_UserId"] = {}; -- Pets go here
	}
	[2] = {  -- Trade Number
		["Player1_UserId"] = {}; -- Pets go here
		["Player2_UserId"] = {}; -- Pets go here
	}
}

With this way the server will know what pets are being traded to who. The downside is that I don’t know how you will be able to code something like this.

2 Likes
      local Trades = {
		[PlayerIdOther] = {
                	Trade = {
				You = { } -- all trade objects
				Other = { } -- others trade items
			}
                }
      }

assuming that trades can be done trough multiple servers you can check trough all the datastores and check if the “PlayerIdOther” is the same ID as the local player if it’s updated in this case it would be something like:

local DataStoreService = game:GetService("DataStoreService")
local trades = DataStoreService:GetDataStore("Trades")

game.Players.PlayerAdded:Connect(function(plr)
      local PlayerIDsTrade = {}
      local succ, tradeTable = pcall(function()
            return trades:GetAsync(plr.UserId)
      end)
      if succ then
            for k,v in pairs(tradeTable) do
                  table.insert(playerIDsTrade, k)
            end
      end
end)

this code piece above will return all the ID’s of the players you are currently trading with. from here you just simply change the datastore of that player if you change your trade and change the datastore of “You” from the player doing the action and the datastore of the other palyer ID’s “Other” so that it updates in sync.

EDIT
of course you could just put it all globally with the dictionairy of both user id’s. its just whatever you prefer

1 Like