How to check which number a tool is associated to

So I am coding a tool to create a charge attack that will release once the button is no longer held down. (For example, if the tool was in the first slot, then the attack will release when you release the one button.) However, the problem is that I am unable to figure out which number the tools are associated to. So, incase someone wanted to reorganize their inventory, and I set the attack to release once the 1 key was released despite the attack being reorganized to the third slot, it wouldn’t work out.
I’ve been looking for solutions for a while and found a thread that did talk about this subject but apparently they were not having the same problem as me. Thanks for reading!

1 Like

Maybe you could track the tools in a table. For example, when a tool is added to the backpack, you can insert it to a table. And the index would be the number.

I have a few questions about this approach before I test it out. (Sorry if I should’ve tested it first but I’m very beginner at coding so it takes a while to research.) First, roblox organizes the tools in the explorer from alphabetical order. (Which is why I just can’t grab a table from the explorer’s backpack and assume its the order they’re in.) Not only that, but the approach you suggested said if a tool was added to the backpack. You can still move around tools in your inventory without equipping them which would result in the game not detecting that it had been either removed or added from the inventory. So, would there be any alternatives or am I just done for here?

I’d just make my own inventory-hotbar system. As far as I understand making your own system would require you to create a way of tracking what tool is in what place, and that would allow you to find what key is connected to what tool.

I’d show a video of my system in work, but my software doesn’t want to be nice right now.

Yeah, you are right, I think you should make your own system, as @Lleventyate said.

To find the associated number for which a tool is assigned, you need only check via GetChildren()

For example, if I have tool banana and tool soda under StarterPack, if I created the banana first, a GetChildren() call would return:

     [1] = banana,
     [2] = soda

You could then get the tool numbers by referring to the indices.

local toolNumbers = {}
for i, tool in pairs(game:GetService("StarterPack"):GetChildren()) do
     toolNumbers[tool.Name] = i

Then, you refer to this table to get the associated tool slot number.

1 Like

I’n really sorry for the late reply as I have been busy for the past week. Though, I do have something to say about this strategy. Yes, it does work almost entirely how I want it to. However, the chart doesn’t change when you move around the tools into a different order. Would this even be possible to accomplish or would I have to take a alternative. (Like preventing the player from moving a tools order or just making the tool activate on click.) (I’m really sorry for the long wait.)

I’m not sure what you mean.
When you say the chart doesn’t change are you saying it doesn’t track where a tool is located, or what key the tool is tied to?

A long, long time ago I put some work into an inventory-hotbar system. You can try out the one here:

Click on this box here:

On the left side there are a bunch of boxes with items, the first 10 or so might look empty because the camera doesn’t display them correctly. Click and hold any of those first 10, move the box onto one of the hotbar boxes (labeled 1, 2, 3, and 4).

Press B to exit the inventory.
Now the hotbar is self-explanatory, just press a number and you’ll pull out the tool.
You can move the item from one hotbar slot to another. click and hold and move. It should be tracked where the tool is and what number it is connected to.

Tell me if this works similar to how you want your system to work.
The system here is old, but I haven’t touched most of it for a long time so if you want me to I can take a look at how it works and explain how I did it.

Sorry if I was unclear about what I was originally trying to say. I am new to both coding and forums so I might not be able to explain some things as well as I would want to. I meant that the chart doesn’t track which number the tool is associated to accurately once you move the tools around. Also, I was going to see what you meant in your game but it seems that I don’t have permission to join right now. Thanks for helping though, I really appreciate it.

Oh, my fault. Try joining now.

I joined the game and checked out what you were telling me to do. I’m not quite sure if this is what I was exactly looking for because I’m trying to figure out which tool is in which slot instead of what I assume is just making a script run when a certain tool is equipped. I’m not entirely sure how the system you made works so I can’t accurately guess if its exactly what I want. Could you explain to me how your game tracks what number the tool is associated to? I think I missed this when I was testing out your game.

  • There’s a dictionary that stores the slots (1, 2, 3, 4) and the tools.
  • When a tool is placed into a slot, the dictionary makes a new key with the slot’s name and sets the key to the tool.
  • When a player presses a number (1 to 4), the script checks the dictionary for the corresponding key.
    The tool is either equipped or unequipped.
  • When the tool is moved to a different slot, the script checks if the new slot is empty.
    If it is empty, the dictionary makes or updates a key and sets it to the tool.
  • The old slot the tool used to be associated to is set to nothing, making it empty.
    Since the old slot is empty, pressing the corresponding key will do nothing.

This is how I keep track of what tool is associated with what number. It’s a bit more work than what I explained, but I think that’s the basic functionality.