Creating unique indexes per inventory item

Simply indexing them by their name is not valid because same weapons will have their own levels and you don’t want them to be mixed up. So how could you specifically index items of the same type but different stats?

Assuming two items with the same name but different stats for example a sword with the name "Simple Sword", this sword can go up to level 3 and at each level the stats are the same as another Simple Sword of the same level. So you won’t have two level 3 Simple Swords with different stats.

As a key you could simply use "Simple Sword LVL3" or "Simple Sword LVL2" to denote the different levels but what if a player picks up another level 3 simple sword, it checks if the player already has one and returns how many the player has. For example you already have one level 3 simple sword with key "Simple Sword LVL3", if you pick up another one it adds an index to the new one ("Simple Sword LVL3_1"), ect.

What happens if a player wants drop/delete "Simple Sword LVL3" and not "Simple Sword LVL3_1", you can just run a check to find the key with the highest index, in this case "Simple Sword LVL3_1" and remove that instead of "Simple Sword LVL3", that way when you pick the same item again it would keep the indexes in order.

TL;DR: You basically just have to think about what makes items of the same type/same name unique, and if you can have multiple of the exact same item, how can you make the item’s key/index unique from items that are exactly the same.

Hope this helps.

Here is an Example how I currently store Unique Items each having there own Ranks & Experience and other Unqiue Data. My method created a ID Index within table instead of making the Inventory Index a UniqueID. Heres my method Below… (Hidden)

My Method

The values are…

E = Experience
R = Rank Or Level
ID = Long String Identification Code. I combine the Unique ID I made for the weapon and a Unique ID generated from HttpService:GenerateGUID

image

The code

Add_Weapon Function I Created in a module
function ArmoryManager:Add_Weapon(Player,ID)
	local Profile = DataManager:GetArmoryProfile(Player) --// Uses ProfileService
	local ArmoryData = Profile.Data --// ProfileService things Indexing Data
	local Arsenal = ArmoryData.Arsenal	 --//Indexing Inventory
	local WeaponAmount = table.getn(Arsenal) --// Getting Amount of Weapons Inside Inventory
	local WeaponTable = Weapons.Get_Weapon_FromID(ID) --// Fetching Weapon Info thats stored ingame Never In datastore
	--print(WeaponTable)
	local WeaponID = WeaponTable.ID  
	
	if WeaponAmount <= 49 then --// Checking if they have less than 49 Weapons within inventroy
		
		local GUID = HttpService:GenerateGUID(false) --// Creating a Unique Identifier
		local Unique_ID = ID.. '#'..GUID --// Combing Weapon ID with Unique Identier
		
		local Weapon = {} --// Creating table that will be Stored within Datastore
		Weapon.ID = Unique_ID
		Weapon.R = 1 -- Rank
		Weapon.SG = {} -- Sigils
		Weapon.E = 0 -- Experience

		
		print(Weapon)
		
		table.insert(Arsenal,WeaponAmount+ 1,Weapon)	
	end
end

Further Explanation into the ID

local Unique_ID = ID.. '#'..GUID

It would typically look like this

“W125#FF4EFE3A-51AB-42BD-AD41-7E6173CFBB98”

First Chunk is a custom ID I give each weapon "W125"
Followed by a “#” so I can use String. split to split em up and find what Weapon it is from weapon ID
here’s an example

local Weapon_ID_Split = string.split(WeaponID ,'#')
local Weapon_ID = Weapon_ID_Split[1]
local Weapon_GUID = Weapon_ID_Split[2]

The Last bit after “#” is the GUID provided HttpService:GenerateGUID

If you read “The code” Hidden segment I added I go in-depth of how I made everything. Down the line i wont be using HttpService:GenerateGUID within the inventories at least maybe in trading

Why I won’t be using it?
As you can see they tend to be very long and I would like to preserve space in the datastore so in the future I will be adding short ID’s like “W125#1” and every time a new copy is added I willy simply add 1 to the Unique ID after “#”

3 Likes