How to detect which tool is actually being used

I have an inventory system I made, and ran into a problem where if the player has more than 1 of the same item in their inventory and they use 1 tool, it doesn’t know which one too take damage from, so they all lose damage, instead of just one

Can see I have 2 equipped


But as I cut down the tree, all 5 axes lose durability.
robloxapp-20191229-1717402

So when a player equips the tool, it gets cloned from ServerStorage, and put on their character, and this functions runs when they hit the tree

local function Hit(hit)
	if not hit:IsDescendantOf(MaterialSources) then return end

	if hit.Parent.Name ~= 'Tree' then return end
	
	local Tree = hit.Parent

	local MaterialHP = Tree:FindFirstChild('HP')
	if not MaterialHP then return end

	MaterialHP.Value = MaterialHP.Value - 1
	Settings.Durability = Settings.Durability - 1
	
	local Player = Players:GetPlayerFromCharacter(Tool.Parent)
	if Player then
		UpdateDurability(Player, Tool.Name, Settings.Durability)
		DurabilityUpdated:FireClient(Player, Tool.Name, 'Tool', Settings.Durability)
	end
end

and this is the UpdateDurability function

return function(player, item, durability)
	local User = PlayerData[player.UserId]
	if not User then return end
	
	for _, v in pairs(User.Inventory.Main) do
		if v.Name == item then
			v.Durability = durability
			print(v.Durability)
		end
	end
end

So here, it does not know which ‘slot’ is selected. Can anyone recommend a fix to this?
If there’s any other piece of code missing that could help someone solve this then please lemme know :grimacing:

Any ideas are welcome

1 Like

All items all called by the same name and you aren’t checking for the item that the player has equipped. Make a value in the player called something like “EquippedSlot” and correlate the slot to the tool.

1 Like

I don’t see how that would work tho. How my inventory is stored is like so

Inventory = {
		Main = {
			[1] = {Type = 'Tool', Name = 'Wooden Axe', Durability = 25},
			[2] = {Type = 'Tool', Name = 'Wooden Axe', Durability = 25},
			[3] = {Type = 'Tool', Name = 'Wooden Axe', Durability = 25},
			[4] = {Type = 'Tool', Name = 'Wooden Axe', Durability = 25},
			[5] = {Type = 'Tool', Name = 'Wooden Axe', Durability = 25}
		},
}

So going

EquippedSlot = {Type = 'Tool', Name = 'Wooden Axe', Durability = 25}

It would still not know which one? Unless I’m missing something here

1 Like
return function(player, item, durability)
	local User = PlayerData[player.UserId]
	if not User then return end
	
	local v = User.Inventory.EquippedSlot
		if v.Name == item then
			v.Durability = durability
			print(v.Durability)
		end
	end
end

If User.Inventory.EquippedSlot is {Type = ‘Tool’, Name = ‘Wooden Axe’, Durability = 25} then all 5 inventory slots will still be there

1 Like

Doing some testing, setting EquippedSlot to be the number, but idk how to get said number from table

print(User.EquippedSlot) -- prints 2
	local CurrentItem = User.Inventory.Main[User.EquippedSlot]
	print(CurrentItem) -- prints table
	if not CurrentItem then return end
	
	if CurrentItem.Name ~= item then return end
	
	CurrentItem.Durability = durability

So how can I get the 2nd item in the Inventory??

Try to equip different axes and you would have your answer.

Each number of the table is unique and if you reduce the durability of only the equipped tool, the other tools should not get damage.

You are storing slots with an index so all you have to do is something like

EquippedSlot = 1
OR
EquippedSlot = Inventory.Main[1]

The second one will directly give you the table of items you need.

2 Likes