Hotbar not showing tools after death

Hello developers,

I recently made a simple hotbar system, and it just works like any other one. Except, it doesn’t update everytime I die or reset my character.

I used the CharacterAdded event at first, it shows me the slot but it gives me this error as well as I can’t equip my tool.

image

My client code:

game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)


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

local backpack = plr:WaitForChild("Backpack")


local tools = backpack:GetChildren()


local slotMax = 8


local hotbar = script.Parent


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


local function updateHotbar()


	for i, child in pairs(hotbar:GetChildren()) do

		if child.Name == "Slot" then child:Destroy() end
	end


	for i, tool in pairs(tools) do


		if i > slotMax then return end


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

		slotClone.Tool.Image = "rbxassetid://" .. tool.TextureId


		slotClone.Parent = hotbar


		if tool.Parent == char then
			slotClone.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
			slotClone.BackgroundTransparency = .25
			slotClone:TweenSize(UDim2.new(.924,0,.127,0), "In", "Quad", .25)
		else
			slotClone.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
			slotClone.BackgroundTransparency = .5
			slotClone:TweenSize(UDim2.new(.6,0,.25,0), "In", "Quad", .25)
		end


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

			if not processed then

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

					game.ReplicatedStorage.Events.EquipToolRE:FireServer(tool, tool.Parent)
				end
			end
		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)


plr.CharacterAdded:Connect(function()
	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)
	
	for i, child in plr.Backpack:GetChildren() do
		if not table.find(tools, child) then
			table.clear(tools)
			table.insert(tools, child)
			
			for i, v in hotbar:GetChildren() do
				if v:IsA("ViewportFrame") then
					v:Destroy()
				end
			end
			
			updateHotbar()
		end
	end
end)

game.ReplicatedStorage.Events.EquipToolRE.OnClientEvent:Connect(function()


	for x, slot in pairs(hotbar:GetChildren()) do


		if slot:IsA("Frame") then

			local tool = tools[tonumber(slot.HotkeyNumber.Text)]

			if tool.Parent ~= char then

				slot.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
				slot.BackgroundTransparency = .5
				slot:TweenSize(UDim2.new(.6,0,.25,0), "In", "Quad", .25)

			else
				slot.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
				slot.BackgroundTransparency = .25
				slot:TweenSize(UDim2.new(.924,0,.127,0), "In", "Quad", .25)
			end
		end
	end
end)

Server script

game.ReplicatedStorage.Events.EquipToolRE.OnServerEvent:Connect(function(plr, tool, parent)
	
	local char = plr.Character
	
	if char then
		
		if parent ~= char then

			char.Humanoid:UnequipTools()
			char.Humanoid:EquipTool(tool)
			
			
		else
			
			tool.Parent = plr.Backpack
		end
		
		game.ReplicatedStorage.Events.EquipToolRE:FireClient(plr)
	end
end)

All of my tools are stored into the StarterGear, it shows that I have the tool in my backpack but I can’t equip it from the hotbar…

Any help is appreciated, thanks.

For those who are experiencing the same problem, I did this for a quick fix. Although there might be a better way to do it, it works for now.

game:GetService(‘StarterGui’):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

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

local backpack = plr:WaitForChild(“Backpack”)

local tools = backpack:GetChildren()

local slotMax = 8

local hotbar = script.Parent

local numToWord =
{
[1] = “One”,
[2] = “Two”,
[3] = “Three”,
[4] = “Four”,
[5] = “Five”,
[6] = “Six”,
[7] = “Seven”,
[8] = “Eight”
}

local function updateHotbar()

for i, child in pairs(hotbar:GetChildren()) do

if child.Name == “Slot” then child:Destroy() end
end

for i, tool in pairs(tools) do

if i > slotMax then return end

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

slotClone.Tool.Image = “rbxassetid://” … tool.TextureId

slotClone.Parent = hotbar

if tool.Parent == char then
slotClone.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
slotClone.BackgroundTransparency = .25
slotClone:TweenSize(UDim2.new(.924,0,.127,0), “In”, “Quad”, .25)
else
slotClone.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
slotClone.BackgroundTransparency = .5
slotClone:TweenSize(UDim2.new(.6,0,.25,0), “In”, “Quad”, .25)
end

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

if not processed then

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

 game.ReplicatedStorage.Events.EquipToolRE:FireServer(tool, tool.Parent)
end

end
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)

plr.CharacterAdded:Connect(function()
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)

for i, child in plr.Backpack:GetChildren() do
if not table.find(tools, child) then
table.clear(tools)
table.insert(tools, child)

for i, v in hotbar:GetChildren() do
if v:IsA(“ViewportFrame”) then
v:Destroy()
end
end

updateHotbar()
end
end
end)

game.ReplicatedStorage.Events.EquipToolRE.OnClientEvent:Connect(function()

for x, slot in pairs(hotbar:GetChildren()) do

if slot:IsA(“Frame”) then

local tool = tools[tonumber(slot.HotkeyNumber.Text)]

if tool.Parent ~= char then

slot.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
slot.BackgroundTransparency = .5
slot:TweenSize(
1 Like

doesn’t work…

charscharscharschars