Custom Hotbar is acting strange

First, when I utilize one of the tools, both darkens (If equipped), and when I un-equip, the other disappeared.

Before:
image

After:
image

The Full Script:

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
repeat
	wait()
until game.Players.LocalPlayer.Character

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")

local GUI = script.Parent
local Background = GUI.Background

local MaxSlots = 9

local Tools = Player.Backpack:GetChildren()

local NumKeys = 
	{
		[1] = "One",
		[2] = "Two",
		[3] = "Three",
		[4] = "Four",
		[5] = "Five",
		[6] = "Six",
		[7] = "Seven",
		[8] = "Eight",
		[9] = "Nine"
	}

function Init()
	
	for _,v in pairs(Background:GetChildren()) do
		if v:IsA("TextButton") then v:Destroy() end
	end
	
	for i, tool in pairs(Tools) do
		
		if i > MaxSlots then return end

		local Slot = script.TextButton:Clone()
		Slot.TextLabel.Text = i
		Slot.Parent = Background

		Slot.Text = tool.Name

		if tool.Parent == Character then

			Slot.BackgroundColor3 = Color3.fromRGB(88,88,88)

		end

		game.UserInputService.InputBegan:Connect(function(Input, GP)
			if GP then return end

			if Input.KeyCode == Enum.KeyCode[NumKeys[i]] then

				game.ReplicatedStorage.EquipRE:FireServer(tool, tool.Parent)
			end
		end)

		Slot.MouseButton1Click:Connect(function()

			game.ReplicatedStorage.EquipRE:FireServer(tool, tool.Parent)
		end)
		
	end
	
end

Init()

Character.ChildAdded:Connect(function(Child)
	
	if Child:IsA("Tool") and not table.find(Tools, Child) then
		
		table.insert(Tools, Child)
		
		Init()
		
	end
end)

Character.ChildRemoved:Connect(function(Child)

	if Child:IsA("Tool") and Child.Parent ~= Character then

		table.remove(Tools, Tools[Child])

		Init()

	end
end)

Player.Backpack.ChildAdded:Connect(function(Child)

	if Child:IsA("Tool") and not table.find(Tools, Child)  then

		table.insert(Tools, Child)

		Init()

	end
end)

Player.Backpack.ChildRemoved:Connect(function(Child)

	if Child:IsA("Tool") and Child.Parent ~= Character then

		table.remove(Tools, Tools[Child])

		Init()

	end
end)

game.ReplicatedStorage.EquipRE.OnClientEvent:Connect(function()
	
	for i,v in pairs(Background:GetChildren()) do
		
		if v:IsA("TextButton") then
			
			local Tool = Player.Backpack:GetChildren()[tonumber(v.TextLabel.Text)]
			
			if Tool and Tool.Parent ~= Character then
				
				v.BackgroundColor3 = Color3.fromRGB(88,88,88)
				
			else
				
				v.BackgroundColor3 = Color3.fromRGB(57,57,57)
				
			end
		end
	end
end)

Help me.

1 Like

The problem is probably because you have your Input Began event inside a for loop. You also start this for loop many times. This is a very VERY bad practice. Instead, at the very least, move the events outside the for loop, if not outside this function. It makes sense to have a .MouseButton1Click event inside a for loop to connect it to all the events but you should not be running this loop multiple times. I will look into your code to see if I can find the reason why but it seems like it was not written very well.

Inside of the Character.ChildRemoved, you’re removing the Tool from the array, even though the Tool went back to Player.Backpack. And you’re removing the Tool when Backpack.ChildRemoved fires, too, even though the Tool may be going to Player.Character. You should only be removing the Tool from the array when it’s destroyed, or not in Player.Character nor Player.Backpack at the same time.

So, you’re Instance.ChildRemoved events should be like this:

local function removeTool(tool)
    -- You may also want to check if 'tool' is a tool.

    if tool.Parent == backpack or tool.Parent == character then
        return -- The player still has the tool.
    end

    table.remove(tools, table.find(tools, tool))
end

character.ChildRemoved:Connect(removeTool)
backpack.ChildRemoved:Connect(removeTool)

This may not be the problem, since you do have Instance.ChildAdded events.