Tools changing slots (Custom toolbar)

Alright, so I made a custom toolbar with keypress, it is practically working, but the problem is, when I equip the a tool from the toolbar, the tools change slots, I think the problem is in the updateBackpack() function, because it adds the things in backpack to the table first and then from the character, but I have no idea how to fix it.

Local script:

local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local char = player.Character or player.CharacterAdded:Wait()
local backpack = player:WaitForChild("Backpack")
local currentKey = 0
local Tools = {}
local Keys = {
	["One"] = "1",
	["Two"] = "2",
	["Three"] = "3",
	["Four"] = "4"	
}


game.StarterGui:SetCoreGuiEnabled("Backpack", false)



local function equipItem(tool, equipped)
	game.ReplicatedStorage.EquipItem:FireServer(tool, equipped)
end


local function check(part)
	for _, tool in pairs(part:GetChildren()) do
		if tool:IsA("Tool") then
			table.insert(Tools, tool)
		end
	end
end

local function updateBackpack()
	Tools = {}
	check(backpack)
	check(char)
	
	   for _, frame in pairs(script.Parent:GetChildren()) do
		if frame:IsA("TextButton") then
			frame.Text = "Button"
		end
	end
	
	for i, toolsInTable in pairs(Tools) do
		local found = script.Parent:GetChildren()[i]
		if found and found.Text == "Button" then
			found.Text = toolsInTable.Name
		end
	end
end

local function keyPressedConvert(slot)
	for _, frame in pairs(script.Parent:GetChildren()) do
		if frame:IsA("TextButton") then
			if frame.Name ~= tostring(currentKey) then
				frame.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
				equipItem(frame.Text, false)
				end
		end
		end
			
	if slot.BackgroundColor3 == Color3.fromRGB(0, 255, 0) then
			slot.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
		currentKey = 0      
		equipItem(slot.Text, false)
	elseif slot.BackgroundColor3 == Color3.fromRGB(255, 0, 0) then
		slot.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
		equipItem(slot.Text, true)
	end
end



local function keyPressedDetect(input, GPE)
	if not GPE then
		for	keypressed, keyframe in pairs(Keys) do
			if input.KeyCode == Enum.KeyCode[keypressed] then
				local found = script.Parent:FindFirstChild(keyframe)
				if found then
					currentKey = tonumber(found.Name)
					keyPressedConvert(found)
				end
			end
		end
	end
end

updateBackpack()

backpack.ChildAdded:Connect(updateBackpack)
backpack.ChildRemoved:Connect(updateBackpack)
char.ChildAdded:Connect(updateBackpack)
char.ChildRemoved:Connect(updateBackpack)
UIS.InputBegan:Connect(keyPressedDetect)

I think this is because when these events fire, the function is called
and it changes the backpack, thus, slots changing

Yeah, I also thought that but I have no idea how to fix it.