In my game, the inventory needs to send requests to the server to perform certain actions, but the way that it’s done is through multiple scripts, and I feel as though it’s unoptimized
Example:
Client calls action
local HeldItem = TheValleyFunctions.GetHeldItem(Player)
TheValleyFunctions Module calls the remote function
function TheValleyFunctions.GetHeldItem(Player)
return RS.Inventory.GetHeldItem:InvokeServer(Player)
end
Server script listens for remote function
IF.GetHeldItem.OnServerInvoke = function(Player)
return InventoryFunctions:GetHeldItem(Player)
end
InventoryFunctions module gets the player’s inventory dictionary and applies function to said dictionary
function InventoryModule:GetHeldItem(Player)
local PlayerInventory = InventoryTables[Player.UserId]
return PlayerInventory:GetHeldItem()
end
Desired code finally runs
function Inventory:GetHeldItem()
return self.HeldItem
end
Try to capture delays in this operation trough print statements. You can check delay by looking at the time shown on the left side of the output. Also, make sure that you don’t have ANY local functions in module scripts because they make it slower
I always cache the inventory on the client so that when the client changes items or their inventory changes, the inventory is both updated on the server and the client. This allows you to use GetHeldItem on the client which would not cause delay.
Basically yes, when the inventory is loaded, you send a copy of the entire inventory table to the client. And whenever there’s a change on the server-side, send that change over to the client. So whenever you need to “get” anything on the client side, you don’t need to wait for a server response.
Would doing this mean I have to replicate the server functions to the client, and then modify them to work with the client-side replication of the inventory?
Yeah, anytime you need to check anything on the client, you’ll just call the client functions directly. Unless it’s a situation where you’re checking the inventory for critical actions (such as using an item); then in this case you should never solely trust the client.