Custom client-server replication concerns

Hey developers,

I have a (perhaps strange) question about my game. In the game, the server keeps track of important data (such as player or NPC stats) and updates it to the client through RemoteEvents. When a player joins the game, they request the current state of the game from the server (through a RemoteFunction), and subsequent changes to state of the game is updated through the RemoteEvents. However, I’m concerned that the client’s state of the game may be incorrect if the following order of events happens:

  1. The client invokes the RemoteFunction on joining the game, and uses the data to construct the state of the game.
  2. The server fires a RemoteEvent, notifying the client of a change.
  3. The client connects the RemoteEvent to a function.

If this were to ever happen, the client’s state of the game would be incorrect, as the RemoteEvent was fired before the client was able to connect to it. Is this ordering of events possible, and how can I make sure this never happens?

  1. The client invokes the RemoteFunction on joining the game, and uses the data to construct the state of the game.
  2. The server fires a RemoteEvent, notifying the client of a change.
  3. The client connects the RemoteEvent to a function

One solution I can think of is swapping the order of 1 and 3. The client should connect a function to the RemoteEvent, then invoke the RemoteFunction. This way, since the RemoteEvent is connected first, any changes are replicated immediately. An issue with this is the RemoteEvent connected function function might need the current data which doesn’t exist yet if somehow the RemoteEvent is fired before the RemoteFunction is invoked, which leads back to the same problem of incorrect order of events.

I can think of a few solutions for this, such as repeat wait() until data , or a “queue” system where any changes replicated before the data has been received by the client is inserted into a table and once the data is received, change the data based on the items in the queue.

This should be able to handle the scenario where the server tries to replicate changes to a player who hasn’t started listening to the RemoteEvent yet. This might not even be needed though, as it might not even be possible that the order of events are wrong in the first place, but it’s better to be safe than sorry, considering this solution shouldn’t take too much work to implement.

For all change-events before the event is registered, you can queue them all up on the server (just shove the data it into some table). After the client connects the RemoteEvent to a function [which you can verify by polling the client continuously from the server until the client pings back successfully], you can then send all the change-events at the same time to the client. This guarantees you are sending the change-events after you confirm client-server communication is established.

From my personal experience, the remote event on the client end (both, actually) will queue until a callback is connected to it. If you bombard the event, you’ll get a warning like, “did you forget to implement OnClientEvent?”