Table.find in a table with dictionaries?

Currently I’m working on a custom backpack/inventory system. Whenever a tool is being added to the backpack, it will check check is the tool already inside the table, if not, it will insert it to the table and create a dictionary. For an example: ToolInstance is the location of the tool, UIButton is the button that was created for the tool, Index is it’s order in the Backpack.

table.insert(BackpackItems, {ToolInstance = v, UIButton = Button, Index = i})

My question is simple: How can I use table.find (or any other substitute way) to check is the same dictionary already exist in the table BackpackItems before adding a new one to the table?

1 Like
local BackpackItems;

local function insert_item(v, Button, i)
    
  local function searchFor(tool_instance)
    for _, dictionary in pairs(BackpackItems) do
      if dictionary.ToolInstance == tool_instance then return true end;
    end

    return false;
  end

  if not searchFor(v) then --if the tool isn't already registered in a dictionary in BackpackItems, add it
    table.insert(BackpackItems, {ToolInstance = v, UIButton = Button, Index = i})
  end
end
7 Likes

This is the easiest way to check. Use the toolInstance as the key for a dictionary. The value of the key will be your sub-dictionary

local inventory = {}
inventory[toolInstance] = {UIButton = foo, Index = bar}

if (inventory[toolInstance[=] == nil) then
1 Like

How about removing the dictionary that was added?

		v.Changed:Connect(function()
			if v.Parent == nil and FindToolInBackpackItems(v) then
				table.remove(BackpackItems, ...)
				s:Destroy()
			end
		end)

@MrAsync provides a good solution to both of these problems, use toolInstance as the key so you can just do

inventory[toolInstance] = nil

for removing it from the table

1 Like

If you are using my method, you won’t need to use table.insert or table.remove. Simply setting the key to nil will remove a value from the dictionary.

inventory[toolInstance] = nil

How’d I access it? It returns nil when I tried to get it with the index.

for i,v in pairs (plr:FindFirstChildOfClass("Backpack"):GetChildren()) do
	if BackpackItems[v] == nil then
		BackpackItems[v] =  {UIButton = s, Index = i}
		print(BackpackItems[1]) -- nil

Because dictionaries don’t have numerical indexes. You must reference the value using the key.

print(BackpackItems[v])

How do I know which tool does the player wants to equip when a keycode is being pressed? For an example, how’d I equip the dictionary with the index 1 when key number 1 is being pressed? Since BackpackItems[1] is no longer usable.

I’m not sure how your system I setup. Normally, the tool is cloned into a the players backpack. Then, that time appears in the backpack bar. Then Roblox handles the equipping (simply the tool is re-parented to the character)

I mean it looks to me like its still an array at the beginning
you were doing table.insert(array, dict)
wouldn’t work in a case like this tho:

Tools = {
   Sword = ........,
   Rifle = ......
}

If you were using the original array you could just compare the toolinstance or name or something to find the right tool
But if you wanted to manually make that connection with the new table you would need to set one up for each tool individually most likely or end up making an array anyway