Hotbar gone wrong

My hotbar not updating when I remove tool from backpack.This is the image what I mean by it .

this image when I equip tool. Works fine

When I clicked Black Sword from Inv the tool was destroyed but the hotbar not.You can see it on row 2

This i when I equip another weapon from inv works fine but row 2 (black sword image) just sit there and the hotbar supposed to only have 2 row but instead the hotbar have 3 row

this is the script:



local plr = game.Players.LocalPlayer
local char = plr.Character

local backpack = plr:WaitForChild("Backpack")


local tools = backpack:GetChildren()


local slotMax = 9


local hotbar = script.Parent


local numToWord = 
	{
		[1] = "One",
		[2] = "Two",
		[3] = "Three",
		[4] = "Four",
		[5] = "Five"
		
	}


local function updateHotbar()


	for i, child in pairs(hotbar:GetChildren()) do
		
		
	    if child.Name == "ImageButton"  then 
		--	if not plr.Backpack:FindFirstChild(child.ItemName.Value) then child:Destroy() end
			
			--if plr.Backpack:FindFirstChild(child.ItemName.Value) then child:Destroy() end
			
			if not plr.Backpack:FindFirstChild(child.ItemName.Value) then 

				child:Destroy()
			elseif plr.Backpack:FindFirstChild(child.ItemName.Value) then child:Destroy() end
				
			
			end
		end


	for i, tool in pairs(tools) do


		if i > slotMax then return end


		local slotClone = script.ImageButton:Clone()
		slotClone.HotkeyNumber.Text = i


		slotClone.Image = tool.TextureId
		
		slotClone.ItemName.Value = tool.Name


		slotClone.Parent = hotbar
		
		


		if tool.Parent == char then

			slotClone.BackgroundColor3 = Color3.fromRGB(255,255,255)
		end


		game:GetService("UserInputService").InputBegan:Connect(function(input, processed)

			if not processed then

				if input.KeyCode == Enum.KeyCode[numToWord[i]] then

					game.ReplicatedStorage.EquipToolRE:FireServer(tool, tool.Parent)			
				end
			end
		end)
		
		slotClone.MouseButton1Click:Connect(function()
			
			game.ReplicatedStorage.EquipToolRE:FireServer(tool , tool.Parent)
		end)
	end
end

updateHotbar()


backpack.ChildAdded:Connect(function(child)
	
	

	if not table.find(tools, child) then
		table.insert(tools, child)
		
		updateHotbar()
	end
end)

backpack.ChildRemoved:Connect(function(child)

	if child.Parent ~= char then
		table.remove(tools, tools[child])
		updateHotbar()
	end
end)


char.ChildAdded:Connect(function(child)

	if child:IsA("Tool") and not table.find(tools, child) then
		table.insert(tools, child)
		updateHotbar()
	end
end)

char.ChildRemoved:Connect(function(child)

	if child.Parent ~= backpack then
		table.remove(tools, tools[child])
		updateHotbar()
	end
end)

There are no error also just it doesn’t update. Any help is appriciated :slight_smile: :slight_smile: