How to save tool properties that are dynamic?

My game has swords that decrease in durability every time you hit a humanoid. This durability value is stored inside the tool itself, using an IntValue (don’t mind the other properties folder, I use it for stable values like damage or armor piercing, while durability is the only property that changes over gameplay).

I am currently saving all tools from a player’s inventory (including swords) in this manner:

players.PlayerRemoving:Connect(function(player)	
		local savedTools = {}
		local currentTools = player.Backpack:GetChildren()

		for i, v in pairs(currentTools) do
			table.insert(savedTools, v.Name)
		end	

		inventoryData:UpdateAsync(key, function(prev)
			print("Saved tools!")
			return savedTools
		end)
end)

And this is how I load all the items:

players.PlayerAdded:Connect(function(player)

	local key = player.Name .. "'s inventory"
	local inventory = inventoryData:GetAsync(key)

	local backpack = player.Backpack

	for i, v in pairs(inventory or {}) do
		local toolName = v

		for i2, v2 in pairs(tools) do
			if toolName == v2.Name then
				local clonedTool = v2:Clone()
				clonedTool.Parent = backpack
			end
		end
	end	
end)

I’m not sure how to begin solving this problem at all. I want to save each durability value for each sword in the backpack accordingly, upon leaving, then load the same durability for each sword, upon rejoining. How would I approach this?

Instead of saving an array of the tool names u can save an array containing a table for each tool.

		for i, v in pairs(currentTools) do
            local toolinfo = {}
            toolinfo.Name = v.Name
            toolinfo.Durability = v.Durability.Value
			table.insert(savedTools, toolinfo)
		end	

and then on ur playeradded can u easily just clone it using the name the set the durability

2 Likes

Thank you! Works just as intended.

1 Like