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