I want to detect if a tool was previously equipped so the toolamount number doesn’t get increased or decreased because it’s not in the backpack anymore.
plr.Backpack.ChildAdded:Connect(function(tool)
if tool:IsA("Tool") then
updateToolAmount(tool, 1)
end
end)
plr.Backpack.ChildRemoved:Connect(function(tool)
if tool:IsA("Tool") then
if not plr.Character:FindFirstChild(tool.Name) then
updateToolAmount(tool, -1)
end
end
end)
local toolWasEquipped = false
plr.Backpack.ChildAdded:Connect(function(tool)
if tool:IsA("Tool") and toolWasEquipped == false then
updateToolAmount(tool, 1)
toolWasEquipped = true
end
end)
plr.Backpack.ChildRemoved:Connect(function(tool)
if tool:IsA("Tool") then
if toolWasEquipped == false and not plr.Character:FindFirstChild(tool.Name) then
updateToolAmount(tool, -1)
end
end
end)
local plr = game.Players.LocalPlayer
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
plr.Character.Humanoid.Changed:Connect(function()
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
end)
local function updateToolAmount(tool, amountChange)
local backpack = script.Parent.Inventory.Backpack
local toolEntry = backpack:FindFirstChild(tool.Name)
if toolEntry then
toolEntry.ToolAmount.Value += amountChange
toolEntry.ToolAmountText.Text = tostring(toolEntry.ToolAmount.Value)
if toolEntry.ToolAmount.Value == 0 then
toolEntry:Destroy()
end
else
if amountChange > 0 then
local template = script.Parent.Inventory.InvTemplate:Clone()
template.Parent = backpack
template.Visible = true
template.Name = tool.Name
template.ToolName.Text = tool.Name
template.ToolIcon.Image = tool.TextureId
template.ToolAmount.Value += amountChange
template.ToolAmountText.Text = tostring(template.ToolAmount.Value)
end
end
end
plr.Backpack.ChildAdded:Connect(function(tool)
if tool:IsA("Tool") then
updateToolAmount(tool, 1)
end
end)
plr.Backpack.ChildRemoved:Connect(function(tool)
if tool:IsA("Tool") then
if not plr.Character:FindFirstChild(tool.Name) then
updateToolAmount(tool, -1)
end
end
end)
local equippedTools = {}
local unequippedTools = {}
plr.Backpack.ChildAdded:Connect(function(tool)
if tool:IsA("Tool") and not table.find(equippedTools, tool) then
updateToolAmount(tool, 1)
table.insert(equippedTools, tool)
end
end)
plr.Backpack.ChildRemoved:Connect(function(tool)
if tool:IsA("Tool") then
if not table.find(unequippedTools, tool) and not plr.Character:FindFirstChild(tool.Name) then
updateToolAmount(tool, -1)
table.insert(unequippedTools, tool)
end
end
end)
I modified my script like this and now it works. You saved my valuable time!
local equippedTools = {}
plr.Backpack.ChildAdded:Connect(function(tool)
if tool:IsA("Tool") and not table.find(equippedTools, tool) then
updateToolAmount(tool, 1)
table.insert(equippedTools, tool)
end
end)
plr.Backpack.ChildRemoved:Connect(function(tool)
if tool:IsA("Tool") then
if not plr.Character:FindFirstChild(tool.Name) then
updateToolAmount(tool, -1)
table.remove(equippedTools, tool)
end
end
end)