Optimizing client<-->server operations

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

This causes noticeable delays in the UI actions

1 Like

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.

2 Likes

Oh wait, he was talking about ping-relayed delay? I’ve just figured it

Are you saying to keep a copy of the inventory on the local script so I don’t have to keep asking the server for permission for everything?

1 Like

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.

Could I then switch over some remote functions to unreliable remote events since I don’t need a server response all the time?

That could cause the client to not be properly synced with the server. Just use normal remotes

1 Like

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?

2 Likes

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.

1 Like

image
I’ll get back to you on how this goes in a while. I’ve got a ton of functions to replicate and modify

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.