Custom backpack

i am trying to make a custom backpack but when i press 1 multiple times it just switches thru my backpack?

code:

	local function ToolEquip(Num)
		local Humanoid = Player.Character:WaitForChild("Humanoid")
		
		local Items = Player.Backpack:GetChildren()
		
		local Tool = Items[Num]
		
		print(Tool.Name)

		if Tool then
			Humanoid:EquipTool(Tool)
		end

		return true
	end

	UIS.InputBegan:Connect(function(Key)
		if Key.KeyCode == Enum.KeyCode.One then
			ToolEquip(1)
		end
	end)```

what do you mean by this? I dont get it

1 Like

presses 1 equips tool1
presses 1 again equips the tool next to it in slot 2
presses 1 again equips the tool next to it in slot 3

1 Like

It swaps through the different tools because you’re referencing the value stored for the given index of the Backpack. When you equip a tool, it’ll be removed from the Backpack and granted to the Character, causing the value/item that was in the second index to correspond with the first index, instead.

Example:


warn(Player.Backpack:GetChildren()) --[[

    [1] = "ExampleSword",
    [2] = "ExampleItem",
    [3] = "AnotherExample"

    --]]

    local Tool = Items[Num] -- In this case, if "Num" is 1, it'll refer to "ExampleSword"

    if Tool then
        Humanoid:EquipTool(Tool) -- Tool is removed from the Backpack, causing the Backpack table to update

        warn(Player.Backpack:GetChildren()) --[[

        [1] = "ExampleItem", -- The next time you click "1", this tool is equipped because it's in the first index
        [2] = "AnotherExample",

    --]]

    end
2 Likes