Custom Hotbar Help

so im trying to make a custom hotbar somemight say i do a repost well its not true because that problem with hotbar was fixed and my current problem is different so when i have 5 tools in my hotbar then when i reset i would only have 4 tools and i dont know how to fix it so here is the code
and i found another bug where i have a ban hammer and when i tried to hit something with ban hammer it breaks the gui too

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


local plr = game.Players.LocalPlayer
local char = plr.Character
local deb = false
local backpack = plr:WaitForChild("Backpack")


local tools = backpack:GetChildren()


local slotMax = 9


local hotbar = script.Parent

local function setEquip(tool)
	
	if tool.Parent ~= char then
		char.Humanoid:EquipTool(tool)
	else
		char.Humanoid:UnequipTools()
	end
	
	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(38,176,222)

			else
				slot.BackgroundColor3 = Color3.fromRGB(13, 62, 79)
			end
		end
	end
end


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


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.ToolIcon.Image = tool.TextureId


		slotClone.Parent = hotbar


		if tool.Parent == char then

			slotClone.BackgroundColor3 = Color3.fromRGB(13, 62, 79)
		end
		slotClone.ToolIcon.MouseButton1Click:Connect(function()
				setEquip(tool)	
		end)
	end
end


local function checkKeyPress(input)
	-- First, we loop through all hotbar slots
	for i=1, #hotbar:GetChildren() do
		-- We check which slot they're pressing, and IF THE SLOT EXISTS (has a tool)
		if input.KeyCode == Enum.KeyCode[numToWord[i]] and tools[i] then
			-- We equip it
			print(i)
			setEquip(tools[i])
			return -- We stop looking at the other slots
		end
	end
end

-- Input connection
game:GetService("UserInputService").InputBegan:Connect(function(input, processed)
	if not processed then
		checkKeyPress(input)
	end
end)
for _, tool in pairs(tools) do
	if not table.find(tools, tool) then
		table.insert(tools, tool)
		updateHotbar()
	end
end

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 and child.Parent ~= plr.StarterGear 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)

here is the model of mine if you wanna take a look

all help is appreciated as im very desperate :sob:

anyone pls help im sooooo desperate for help i havent slept in three days

Hey! I recently made a custom hotbar. I’ve decided to make it open source for you and others. Here’s the link:
https://www.roblox.com/games/6818292263/Hotbar
Hope this helps you. :smiley:

3 Likes

thx imma look into it now :slight_smile:

1 Like

it worked and i like the animation too!!
i will credit you!

It doesn’t have renders, that’s up to you.

If you wanna add hotbar Icons just add this at lines 34, 134 and 135 in the Module script
Add an ImageLabel in the template which is in Replicated storage and scale it and name it to Icon or whatever you want…

"ImageLabel"; --Line 34 --Allows the ImageLabel into the template

local iconInd = new_template:FindFirstChild("Icon") --Line 134 --Change "Icon" to the name of your imagelabel

iconInd.Image = tool.TextureId --Line 135 --makes it so the Icon is = to the tools texture ID

And just change the Texture ID in your tool to what ever you want and your good to go