Custom toolbar doesn't work/is very buggy

I’m trying to make a custom toolbar for my fps game, but it is very buggy and doesn’t work, and I have no idea on how to fix, or change it.

The local script is stored in starterplayerscripts

    local hotbar:Frame = HUD.Hotbar
	local TemplateW:Frame = hotbar.TemplateW
	local TemplateT:Frame = hotbar.TemplateT
	TemplateW.Visible=false
	TemplateT.Visible=false
	TemplateW.UIStroke.Enabled=false
	TemplateT.UIStroke.Enabled=false
	local tools = player.Backpack:GetChildren()
	local slotMax = 9
	local numToWord = {
		[1] = "One",
		[2] = "Two",
		[3] = "Three",
		[4] = "Four",
		[5] = "Five",
		[6] = "Six",
		[7] = "Seven",
		[8] = "Eight",
		[9] = "Nine"
	}
	local function updateHotbar()
		for _, child in pairs(hotbar:GetChildren()) do
			if child:IsA("Frame") and child.Name == "Slot" then
				child:Destroy()
			end
		end
		for i, tool in ipairs(tools) do
			if i > slotMax then break end
			if tool and tool:IsA("Tool") then
				local slotCreated = false
				for _, Folder in ipairs(ReplicatedStorage.Weapons:GetChildren()) do
					for _, SubFolder in ipairs(Folder:GetChildren()) do
						if ffc(SubFolder, tool.Name) and ffc(tool,"ACS_Settings") then
							local Weapon = wfc(SubFolder, tool.Name)
							local WeaponType = require(Weapon.ACS_Settings).WeaponType

							local Clone = TemplateW:Clone()
							Clone.Name = "Slot"
							Clone.ToolName.Text = tool.Name
							Clone.Visible = true
							Clone.HotkeyNumber.Text = i
							Clone.Parent = hotbar
							Clone.Image.ImageLabel.Image = Temp(player.Character)
							Clone.LayoutOrder = i

							slotCreated = true
							break
						end
					end
					if slotCreated then break end
				end

				if not slotCreated then
					local Clone = TemplateT:Clone()
					Clone.Name = "Slot"
					Clone.ToolName.Text = tool.Name
					Clone.Visible = true
					Clone.HotkeyNumber.Text = i
					Clone.Parent = hotbar
					Clone.LayoutOrder = i
				end
				UserInputService.InputBegan:Connect(function(input, processed)
					if not processed then
						if input.KeyCode == Enum.KeyCode[numToWord[i]] then
							ForceEquipToolRE:FireServer(tool, tool.Parent)			
						end
					end
				end)
			else
				print("Invalid tool at index " .. i)
			end
		end
	end
	updateHotbar()
	player.CharacterAdded:Connect(function(character)
		tools = player.Backpack:GetChildren()
		updateHotbar()
		--[[HOTBAR]]
		player.Backpack.ChildAdded:Connect(function(child)
			if not table.find(tools, child) then
				table.insert(tools, child)
				updateHotbar()
			end
		end)
		player.Backpack.ChildRemoved:Connect(function(child)
			if child.Parent ~= character then
				table.remove(tools, tools[child])
				updateHotbar()
			end
		end)

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

		character.ChildRemoved:Connect(function(child)
			if child:IsA("Tool") and child.Parent ~= player.Backpack then
				table.remove(tools, tools[child])
				updateHotbar()
			end
		end)

		ForceEquipToolRE.OnClientEvent:Connect(function()
			for _, slot in pairs(hotbar:GetChildren()) do
				if slot:IsA("Frame") and (slot~=TemplateT or slot~=TemplateW) then
					local tool = tools[tonumber(slot.HotkeyNumber.Text)]
					if tool.Parent ~= player.Character then
						slot.UIStroke.Enabled=false
					else
						slot.UIStroke.Enabled=true
					end
				end
			end
		end)
	end)

Problems I have:

  • When equipping a weapon e.g from slot 1, all other slots under it get deleted such as slot 2 and 3.
  • Unable to equip tools