So basically I have this script that adds a tool into this table from ReplicatedStorage
{ ["Basic Axe"] = 1, ["Wood"] = 1 }
and that works but whenever I equip a tool it leaves the players backpack and when I unequip it, it gets readded to the backpack making how many Basic Axes I have go up to 2 when I only ever picked up 1
{ ["Basic Axe"] = 2, ["Wood"] = 1 }
and if I spam equip and unequip it, it just keeps adding up, any ideas on what to do would be very appreciated
Full code if needed:
local replicatedStorage = game:GetService("ReplicatedStorage") local player = game.Players.LocalPlayer wait(1) --Count Variables local counts = { } --Add all game items/resources to the counts variable for i, child in ipairs(replicatedStorage.Inventory:GetDescendants()) do if child:IsA("Tool") then counts[child.Name] = 0 end end --Check what is in player inventory player.Backpack.ChildAdded:Connect(function() local inventory = player.Backpack:GetChildren() for i, child in ipairs(inventory) do if counts[child.Name] then counts[child.Name] = counts[child.Name] + 1 end end print(counts) end)
edit: I found a new problem where if I pick up 5 logs, it says I have 11 as described up before with the stuff leaving my backup, but if I equip one of those logs and unequip it then it goes from 11 logs to 16
The reason an item gets removed from the Backpack when you equip it, is because tools are generally equipped by being moved into your Character.
Though, the actual issue here is that you don't clear the counts
table.
local function clearcounts() for i, child in ipairs(replicatedStorage.Inventory:GetDescendants()) do if child:IsA("Tool") then counts[child.Name] = 0 end end end clearcounts() player.Backpack.ChildAdded:Connect(function() local inventory = player.Backpack:GetChildren() clearcounts() for i, child in ipairs(inventory) do if counts[child.Name] then counts[child.Name] = counts[child.Name] + 1 end end print(counts) end)