How to detect an old parent

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)
2 Likes
2 Likes

I don’t know how to apply this.

1 Like

Can’t you just count the childrens?

local count = #plr.Backpack:GetChildren()

tool.AncestryChanged:Connect(function(OldParent,NewParent)

I want children of the same tools shall I use a loop for that?

Do you mean something like this?

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)

I had tried this before and when a new tool was added to my backpack it wasn’t cloned in the inventory gui

I mean how the whole script should be

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)

Try using a table instead then.

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)

When I unequip the tool the toolamount increases

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)

Sorry I wasn’t around after my last response. Glad I could help though.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.