Best datastore method for saving duplicate tools

So I am currently deciding on whether to continue to serialization for my datastore. I am using Datastore2, and the serialization method. The only problem I face is using serialization would it possible to store duplicates of something, or would it be much better to use a empty table. Unless in the table which using serialization I added something called ["Quantity"] = 1, and increase it if they get a duplicate. So I have a weapon which has it’s own personal stats in which the player can upgrade it, using the serialization method I would have to make each serial for each upgrade which sounds inefficient, and for table I’m not sure how to add things to it and set them, I guess it would be to get the empty table, and do

table.Insert(Table_name, {
        ["Name"] = "Wooden Sword",
        ["Owned"] = true
        -- So on for all the stats
}) 

datastore2:Set(Table_name)

The only problem I face with I don’t know how to retrieve this data and put it into a sword. My current method is:

for num, item in pairs(ReplicatedStorage.Weapons:GetChildren()) do  -- Checks all the weapons in the weapons folder
		for serial, name in pairs(Weapons.WeaponsTable) do -- Checks table
			if item.Name == name["Name"] and name["Owned"] == true then -- if they own it
				adding.AddedEvent(item, "Weapons", player)
			end
		end
	end

As you can see if someone was to join the game there data would be incorrect as it doesn’t in count for an upgrade weapon. There is also something else which may be hard to implement and that is an equipped save, so when the leave there equipped weapon should save as the equipped form. I have an idea for how to do this but may be incorrect:

table.Insert(Equip_Table, Weapon, { 
            ["Name"] = "Wooden Sword",
            ["Equipped"] = true,
           -- So on for all the stats
}) 

datastore2:Set(Equip_Table)

So what I changed in this table.Insert was I added a weapon into the insert which would go to a predefined table with ["Weapon"] = {}.

Having this could solve an issue of mine, so I have an equip feature in my inventory so when it is equipped it will update the value, and so nothing else could be equipped while something is equipped, I will do this by checking the table which holds the equipped items and would see if there is already something equipped, and if not then equip the weapon. So hopefully you understand where I am going with this, so what I really want to know what would be the best way to approach this? How would I save stats on a weapon? would that be how I set a table? Thank you for reading this. Any Help would be appreciated. Thank you! :heart:

4 Likes

Ok so for your quantity issue that could be resolved by doing what you said, for the equipped issue I would not manually add an Equipped field to every entry in the table since I assume that it is some sort of inventory, you would be wasting valuable datastore space, instead I would have a field in the main datastore table that stores the index of the equipped weapon in the inventory table.

1 Like

So something like

equip_table = {
     ["Weapon"] = 5, --Index
     ["Armour"] = 3, -- Index
    -- So on
}

so when some joins it will cycle through the weapons table, and look for the index which is assigned in the equip_table.

One quick question, how would I assign it, so when I equip an item how would it know what serial number to save, would I have a intValue inside the weapon with the serial number.

After you add the weapon to the inventory just get the index of it by finding the length of the table, for example if the “WoodenSword” is equipped, then after you add it just find the length of the table (#table)

Also, to save more space normally for inventory system I would have an Item id which would be a few digits, as opposed to saving the name. That way in the future if you decide the change the name your datastore won’t be broken, also, it will save a lot of space. In addition, I normally have a module with datastore table keys, for example something like this:

--the datastore inventory Keys module:

local DatastoreInventoryKeys = {}

DatastoreInventoryKeys.Id = "1"
DatastoreInventoryKeys.Quantity = "2"

return DatastoreInventoryKeys 

--then when your loading or saving the data you can just use the module:

table.Insert(Equip_Table, Weapon, { 
            [DatastoreInventoryKeys.Id] = "1",
            [DatastoreInventoryKeys.Quantity] = 2,
           -- So on for all the stats
}) 

This way you won’t make any mistakes when referencing specific keys, and you can save even more space by shortening the Key length of the dictionary, and still make it readable.

Do you have to add a quantity in this DatastoreInventoryKeys? If it’s just the id’s that you need.

This is my current layout:
image
Would I just add another module script and then require it in the main script.

I added because you said you wanted the best method, if you just duplicate the same entry you will be wasting memory. If you plan on having a moderate inventory size you most likely won’t have to, and yes you can just require it like that.

The inventory size is going to be 30 by default, and then 60 with a gamepass. Would that be considered moderate.

Yes, and also will you just be saving the items’ Ids or are there any additional fields.

local Weapons = {}

Weapons.WeaponsTable = {
	
	[1] = {
		["Name"] = "Wooden Sword",
		["Owned"] = true,
		["Damage"] = 5,
		["Magic Damage"] = 2,
		["Health"] = 0, 
		["Rarity"] = "Common"
	},
	
	[2] = {
		["Name"] = "Stone Sword",
		["Owned"] = false,
		["Damage"] = 5,
		["Magic Damage"] = 2,
		["Health"] = 0, 
		["Rarity"] = "Common"
	},
	--[2].. [3].. [4].. and so on.
	
}



return Weapons

This is my current weapon inventory, so I think I’ll just save the ids, of weapon, armour and potions.

Yes that should be fine, so you can have a different table for the each and then just directly insert the id, instead of a table. Ensure you combine the datastores.

Datastore2.Combine("Inventory", "Armour", "Weapons","Stats","Potions")

local function GetData(player)
	local armourStore = Datastore2("Armour", player); local armourStoreData = armourStore:GetTable(Armour.ArmourTable)
    local potionsStore = Datastore2("Potions", player); local potionsStoreData = potionsStore:GetTable(Potions.PotionTable)
	local weaponsStore = Datastore2("Weapons", player); local weaponsStoreData = weaponsStore:GetTable(Weapons.WeaponsTable)
	local statsStore = Datastore2("Stats", player); local statsStoreData = statsStore:GetTable(Stats.StatsTable)

This is my current layout. and to set them all I do is weaponsStore:Set(weaponsStoreData) when I need to update a table.

Perfect that is exactly what I meant.

Thank you for your help! :heart:

1 Like