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?
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
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
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