Custom Hotbar Troubles

So I have been making a hotbar,

When spawning the tools into the custom gui I have to name them the index of the tool so the number enabling works efficiently. but I ran into a dilema. for some reason roblox is indexing the tools as 3 onwards instead of starting at 1. so when going ingame to enable tools the tools start at 5,6,7 instead of 1,2,3.

Please can someone help!

for __, tool in pairs(Player.Backpack:GetChildren()) do
	if tool:IsA("Tool") then
		local Ref = script.Template:Clone()
		Ref.Name = __
		Ref.Text = tool.Name
		Ref.Parent = script.Parent.Frame
		Ref.Visible = true
		
		tool.Equipped:Connect(function()
			local Found = Ref
			local tweenOn = TweenService:Create(Ref.Round, TweenInfo.new(0.2), {ImageColor3 = Color3.fromRGB(85, 170, 255)})
			local tweenOff = TweenService:Create(Ref.Round, TweenInfo.new(0.2), {ImageColor3 = Color3.fromRGB(30, 30, 30)})				
			tweenOn:Play()	
		end)
		
		tool.Unequipped:Connect(function()
			local Found = Ref
			local tweenOn = TweenService:Create(Ref.Round, TweenInfo.new(0.2), {ImageColor3 = Color3.fromRGB(85, 170, 255)})
			local tweenOff = TweenService:Create(Ref.Round, TweenInfo.new(0.2), {ImageColor3 = Color3.fromRGB(30, 30, 30)})				
			tweenOff:Play()	
		end)		
		
		Ref.MouseButton1Down:Connect(function()		
			if Player.Character:FindFirstChild(Ref.Text) then
				Player.Character.Humanoid:UnequipTools()
			else
				Player.Character.Humanoid:EquipTool(Player.Backpack:FindFirstChild(Ref.Text))
			end
		end)
	end
end

Isn’t it a custom hotbar? Aren’t you the one doing the indexing?

I am just using the indexing from the backpack which is automatic. but disabling the backpack. it is working but for some reason starting at 5,6,7 not 1,2,3

Interesting, I don’t have an idea on how to fix that, but perhaps, you can name them according to the indices starting from 5 instead of 1, maybe by doing n+4.

If you want the first index, it would be 1+4, second 2+4 ect.

You’re probably having problems because of this line right here:

if tool:IsA("Tool") then

Even though your tool instances are filtered out, your automatic numbering is not, causing the numbering to be uneven, and basically giving you a result like this for your loop:

index tool type
1 not Tool
2 not Tool
3 not Tool
4 not Tool
5 Tool
and so on.

The easiest way to fix this is to create your own index counter and using that instead:

local i = 0
for __, tool in pairs(Backpack) do
    if tool:IsA("Tool") then
        i = i + 1
        -- make gui from `tool` and `i`
    end
end
2 Likes