Requiring self on the client

I want to get ‘self’ on the client.

I ran a ‘.new’ method on the inventoryManager module and returned self. I want this self on the client, not a copy, the live version of ‘self’, so the client knows whats in the inventory at all times.

I’ve already tried requiring a module which has a ‘local inventories = {}’ and adding self to the table everytime ‘.new’ is ran, but whenever i print the inventories table in a ‘returnInventory’ function, it prints ‘{}’.

I’ve tried firing a remote event with self but its not a live copy.

inventoryManager (module in replicated storage)

inventoryManager.inventories = {}

function inventoryManager.new(player)
    
  local self = setmetatable({}, inventoryManager)
      
  self.inventory = {}
  self.equipHotbar = {
      [1] = "",
      [2] = "",
      [3] = "",
      [4] = "",
      [5] = "",
      [6] = "",
  }
      
  self.player = player
      
  inventoryManager.inventories[player.UserId] = self
  print(inventoryManager)
      
  return self
end


function inventoryManager:getData(player)

    -- ran from client
    
    print(inventoryManager.inventories)
    
    for i, inventory in pairs(inventoryManager.inventories) do
        if i == player.UserId then
            print(inventory)
            return inventory
        end
    end
end

Any help appreciated

Metatables cannot cross the network boundary.

Ah, makes sense, how would you suggest me getting all the inventory info over to the client then?

Potentially some system to only update the client’s information when events happen with an item on the server. Getting an item, update visuals/information.

Ah, so for example if i have the inventoryModule full of modules, for example: inventory:add(), inventory:remove(), ext. I fire a remote event to the client with ‘self’ so it can update whatever is necessary?

Since self is a metatable no, but whenever you Add or Remove an item you could notify the client of these specific changes. A simpler way could be sending a table of items (not an metatable) and looping through them on the client, only with the downside that you are sending large payloads between the Client and Server. The simple solution could go poorly the more inventories and items that are in a game.

Hmm. I’m not experienced with inventories, do you know of any better alternatives performance wise?

Well with the notifying the client when adding or removing, it can be very performant. Items can be cached on the client, only a few changes are tracked, and you can minimize data being sent. The only major things are identifying items that essentially keep inventory changes synced across the boundary.

I think @OblekfacePC’s solution is the best here. I use ProfileService in my games and use the same solution for the client to know the player’s data. I also use the same system for other things here and there.

  • When the player joins, fire a RemoteFunction from the client to the server to receive their current hotbar data.
  • Then every time you make a change to the data on the server, fire a RemoteEvent from the server to that client to update their copy.
  • Basically you’re making a clone of the inventory and/or equipHotbar tables on the client and syncing it with the server.

Alright, I will try this way, thank you both for helping, I will mark the solution when tested.