Finding a Table from it's properties

I’ve been using OOP to create an inventory system. The issue is that I need a way to find it outside of the Module Script creating it.

The solutions I’ve tried seem very hacky and unoptimized.

My first solution was creating a separate table and having all the OOP Objects be stored in there.

local Inventory = {}
local All = {} -- the storage table.
  local self = setmetatable({}, Inventory)
  self.Owner = owner
  table.insert(All, self) -- storing the object.
  return self

function Inventory:Find(owner)
  local foundInventory
  for _, inv in pairs(All) do

    if inv.Owner == owner then -- checking objects for a matching owner.
      foundInventory = inv break

  return foundInventory

return Inventory

Another solution I’ve thought about is using a BindableEvent paired with a RemoteEvent, but, again, it also seems like a very hacky solution.

If this is the correct solution, tell me it is. Other than that, please inform me of the best way to do this.

I’m fairly certain that storing all of them in a table is the best way to go, I would use the player as the key and the inventory object as the value. If you want to make one that’s better in terms of organization, you could create another module and store all of the active inventories like that.


Module 1:

local activeInventories = require(script.Parent:WaitForChild('ActiveInventories'))

local inventory = {}
inventory.__index = inventory = function(owner)
    local self = setmetatable({}, inventory)
    activeInventories[owner] = self
    return self

inventory.Find = function(self, owner)
    return activeInventoires[owner]

return inventory

Module 2, which is called ActiveInventories, would just be a standard empty module script:

local activeInventories = {}

return activeInventories