I am making an inventory system and the only way I can think of doing it is comparing two tables and if they have the same item I will clone the item. Is this the best way to compare the tables or am I going about this wrong?
for index, value in pairs(Inventory.PlayerInventory) do
for index1, value2 in pairs(Inventory.ItemData) do
if value == Inventory.ItemData[index1].Name then
local cloneitem = game.ReplicatedStorage.Items[Inventory.ItemData[index1].Name]
cloneitem:Clone()
cloneitem.Parent = workspace
end
end
end
But it depends, anyways, my idea is to store additional information in each item whether it is the type of item that has to be cloned or whatever the OP wants to do with it. Then the complexity of the algorithm goes from O(n^2) to O(n) (a lot faster but takes more memory), because you won’t have to make two loops, just one over the inventory.
I assume that what you’re trying to do is to look at all the items in the PlayerInventory table and then find their data in the ItemData table. If so, then what I would do is to change your ItemData from an array of item data to a dictionary of item data.
Now, all you have to do to find the data for a Player’s Inventory item is to index the dictionary.
for index, value in pairs(Inventory.PlayerInventory) do
for index, value in pairs(Inventory.PlayerInventory) do
if not Inventory.ItemData[value] == nil then -- make sure the item is in the ItemData table
local cloneitem = game.ReplicatedStorage.Items[Inventory.ItemData[value].Name]
cloneitem:Clone()
cloneitem.Parent = workspace
end
end