Custom Toolbar only working 1 time and then breaking

Ok, so I tried to make a custom toolbar GUI, It works when I equip it and de-equip it for the first time, but when I try it again it just doesn’t work, I tried to debug it by printing every time I pressed the GUI and it also only printed when I equipped and de-equipped it for the first time.

Local script here:

local gui = script.Parent
local player = game.Players.LocalPlayer
local backpack = player.Backpack
local char = player.Character or player.CharacterAdded:Wait()
local HasEquipped = false

local function disableBackpack()
	game.StarterGui:SetCoreGuiEnabled("Backpack", false)
end


local function getThingsInBackpack()
for i, tool in pairs(backpack:GetChildren()) do
	if tool:IsA("Tool") then
		if not char:FindFirstChild(tool.Name) then
		local newgui = Instance.new("TextButton")
		newgui.Parent = gui
		newgui.Text = tool.Name
		newgui.Name = tool.Name
		newgui.Size = UDim2.new(0, 200, 0, 50)
		end
	end
end
end


local function refreshBackpack()
	for i, frame in pairs(gui:GetChildren()) do
		if frame:IsA("TextButton") and not char:FindFirstChild(frame.Name) then
		frame:Destroy()
		end
	end
end

local function equipItem()
for i, tool in pairs(gui:GetChildren()) do
	if tool:IsA("TextButton") then
       tool.MouseButton1Click:Connect(function()
		if not HasEquipped then
		HasEquipped = true
		print("equiping")
		game.ReplicatedStorage.GiveItems:FireServer(tool.Name)
			else
		HasEquipped = false
		print("unequiping")
	game.ReplicatedStorage.RemoveItems:FireServer(tool.Name)
end
end)
end
end
end




refreshBackpack()
getThingsInBackpack()
disableBackpack()
equipItem()

backpack.ChildAdded:Connect(function()
	refreshBackpack()
	getThingsInBackpack()
end)

backpack.ChildRemoved:Connect(function()
	refreshBackpack()
	getThingsInBackpack()
end)

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char.Humanoid.Died:Connect(function()
			wait(4)
			refreshBackpack()
			getThingsInBackpack()
		end)
	end)
end)```

Could you perhaps provide a visual example as well? Like a video that might make it easier to understand it.

Here you go: robloxapp-20200603-0923483.wmv (294.4 KB)

Ok, I tested it a bit out and found out that the problem is when I run the function refreshBackpack(), but I still don’t know how to fix it.