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
But as I cut down the tree, all 5 axes lose durability.
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
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.
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
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