Backpack tool index needed

I want to suffle between tools when I press a single key on my xbox controller.
Sadly the ONLY way of doing so, is to create my own toolbar gui. Because there is no way of getting the index of a tool in the backpack!

If I just get the children of backpack, and shuffle between tools[1] → tools[2] then it might select 2nd tool then the 5th tool. Then tools[3] might be the first tool (in roblox own backpack gui)

So could there be a function like: player.Backpack.Tool:GetIndex()?

1 Like

Isn’t the index just the order of the tool in the backpack?

local order = {}

for i,v in pairs(backpack:GetChildren()) do
order[v] = i
end

print(order[tool])

2

print(backpack:GetChildren()[2])

tool

No, thats the thing. Its not the order of getchildren() table

Oh – I see. The backpack assigns their index to the first available index as they enter the backpack/character. It would be nice if ROBLOX had a get index function – I see. However, if you really want to get the index now, you could do:

local tools = {}

function toolAdded(tool)
for _,v in pairs(tools) do
if v == tool then
return
end
end
table.insert(tools, tool)
local event; event = tool.AncestryChanged:connect(function()
if tool.Parent ~= character and tool.Parent ~= backpack then
for i,v in pairs(tools) do
if v == tool then
table.remove(tools, i)
break
end
end
event:disconnect()
end
end)
end

player.Backpack.ChildAdded:connect(function(child)
if child:IsA("Tool") or child:IsA("Hopperbin") then
toolAdded(child)
end
end)

player.Character.ChildAdded:connect(function(child)
if child:IsA("Tool") or child:IsA("Hopperbin") then
toolAdded(child)
end
end)

And there’s your list of tools with the same index order that the backpack uses.

Thanks, I’ll use that while I await this index function. But this doesn’t work if the player moves the tools in the backpack.

I agree that something like this is needed. It would be helpful if backpack:GetChildren() returned the items in the same order as listed in the action bar. Or alternatively, provide an API for the action bar, such as actionBar:GetChildren().

The use case that I have is that I want to save the action bar items, in the exact order that the user has them in, and then restore them later. Currently this is not possible, since backpack:GetChildren() returns the items in an order that may be different than the order displayed on the action bar.

1 Like