What happens if a player sent through a remote leaves while the signal is being sent?

I am returning a table of players to the client upon a RemoteFunction invocation. This table is then being stored on the client. Players are removed from the client-side table on a client-side Players.PlayerRemoving to avoid a memory leak.

A memory leak would occur if a player had already left the game by the time they are stored to the client-side table, as Players.PlayerRemoving would fire too early and so the player would never be removed from the table.

Is there any possibility that a player could leave the game at just the right time such that the player instance is not nil when it reaches RemoteFunction.OnClientInvoke but Player.PlayerRemoving has already fired by that point, causing a memory leak in my case?

If I’m not mistaken, when a Player leaves while invoking the server/server invoking client the RF errors and I assume roblox will take care of your issue.

My worry isn’t that the client that invoked the server may leave, but that one of the players inside the table returned to the client may leave.

Here’s what I’m doing for an inventory script I’m making right now

  1. a change is detected from the __newindex metamethod on the players inventory table
  2. it sends it over to another script called RemoteHandler, which JSON Encodes it and sends it to the client
  1. The client decodes it and stores it in a modulescript

This uses REs, not RFs. This way clients shouldn’t have to invoke the server.

What you could do instead in 1. is on RunService.Heartbeat check if game.Players:GetPlayers() is equal to the table you have (maybe use the __eq metamethod). If not then fire an RE to all clients that has a new table of players.

I think the PlayerRemoving & RemoteEvent event signal order will be deterministic as the player-removed replication should arrive in the same packet.

Even if this is the case, if the PlayerRemoving event fires before OnClientInvoke is invoked, will the what-would-be instance of the player that was removed be nil when it is passed to OnClientInvoke?

I don’t think that it would be ensured that the OnClientInvoke Player reference is nil.

1 Like

Do you have any sources you could provide that you’re basing this on?

I am basing this mostly on network analysis and my knowledge of how instantiating/deletion is replicated over the network. If the OnClientInvoke is fired before PlayerRemoving, but the OnClientInvoke packet does not arrive before PlayerRemoving is replicated, then you can have a case where the Invoke contains reference to the player that was subsequently removed.

I don’t know how often your RemoteFunction is invoked, so I can’t be certain if it would deterministic relative to the PlayerRemoving replication.

2 Likes