Perplexed on how to structure PlayerData for stacks and trade

Hello Developers!

In my player data table I keep references to the knife/gun models.


export type PlayerData = {
	
	Inventory: {
		Knifes: {[number]: 
			{ 
			["Model"] : string, 
			}
		},
		Guns: {[number]: 
			{
			["Model"] : string, 
			}
		},
	},
	EquippedKnife: string,
	EquippedGun: string,
	
}

local DEFAULT_PLAYER_DATA: PlayerData = {
	Inventory = {
		Knifes = { 
			[1] =  {
				["Model"] = "Default",
			},
			[2] = { 
				["Model"] = "Formosa", 
			}
		},
		Guns = { 
			[1] =  {
				["Model"] = "DefaultGun",
			},
		},

	},
	EquippedKnife = "Default",
	EquippedGun = "DefaultGun",
	
}

In the future I want players to be able to have multiple items of the same knife/gun, for example having x10 Formosa knives. As well as including a trading system. I also want to keep track of each item that is currently circulating in game.

My issue is that I do not know the best way to structure the player’s data.

I have thought about using UUID for the use of trading, tracking each individual knife/gun but It also seems to be quite expensive to do? Storing it in a table as such. With using UUID I also don’t really know how to structure the data to allow stacks of the same item,

For reference, I also have a server module script that stores all “data” of the knives and guns for it to be easily maintained and alter.

local ItemDataTable: {[string]:ItemConfig.ItemData} = {
	
	Default = {
		Name = "Default", 
		Type = "Knife",
		Rarity = "Common", 
		Tradable = false
	},
	Sycth = {
		Name = "Sycth", 
		Type = "Knife", 
		Rarity = "Divine", 
		Tradable = true
	},
	Formosa = {
		Name = "Formosa",
		Type = "Knife", 
		Rarity = "Divine", 
		Tradable = true
	},
	DefaultGun = {
		Name = "Default", 
		Type = "Gun", 
		Rarity = "Common",
		Tradable = false
	}
}

I am just very conflicted on how to structure all of this.
I appreciate any form of help :pray:

Thank you!

I’d suggest you use the method you mentioned, except store the amount as a value and the string the key. Something like this:

local DEFAULT_PLAYER_DATA: PlayerData = {
	Inventory = {
		Knifes = { 
			["Default"] =  {
				["Amount"] = 1,
			},
			["Formosa"] = { 
				["Amount"] = 2, 
			}

}

I see…but how should I go about structuring the data for trading/tracking the amount of items in game?

While trading, you can use the same structure:

			["Formosa"] = { 
				["Amount"] = 2, 
			}

If the player wants to add or decrease the amount of knives/guns they put in the trade you can just modify the Amount value.

okay, and how should I go about tracking the amount of items in circulation?

Just use a simple loop

local itemsInTrade = {
   player1 = {},
   player2 = {}
}

local count = 0
for _, side in pairs(itemsInTrade) do
   for _, item in pairs(side) do
      if item and item.Amount then
         count += item.Amount
      end
   end
end