How to reduce latency in-game

If you see anything that needs to be edited (keywords, grammar, misconception, etc), don’t hesitate to tell me, i’m no advanced informatician and english is not my primary language.

Seeing I’ve never seen any topic about how to directly deal with latency in-game, I’m using my current experience and knowledge to explain how latency can affect player experience and how to reduce it in Roblox games.

I’ve also personally seen a few games on the popular sort with those problems, that’s another reason why I wanted to make this tutorial.

  1. What is latency?

Latency is the rate at which information travels in the network, from the server and the client and vice-versa, the lower the latency, the faster the information travels.

Latency depends on your internet connection and the server ping, if you have a great internet connection but still experience higher latency, this is not neccessarily a problem on your side. (I’m talking about video games in general here)

Higher latency can result in a lack of accuracy between clients, for exemple, if you play an online shooter game, go behind cover but still die from being shot at, it’s because your position is not immediatly transfered to other players, so you’re still a few feets behind from your actual position while moving.

In Roblox games, if the work is not correctly distributed to the client and the server, this can result in a few problems:

-Security issues, too much work done by the client, especially the important data
-High latency, too much work done by the server, especially the constantly replicating data

  1. What exactly causes latency to rise?

The exact reason why latency can rise is replication, replication is when the server shares information to all the clients, sharing big chunks of informations very often causes the latency to go up.

When Filtering Enabled was still an option, having it off caused replication to be automatic for the game, you could just make create all the stuff on a local script and handle a few stuff on the server, now, the developers have to manually handle the replication, through the use of Remote Events and Remote Functions.

  1. How to deal with latency?

To avoid latency going up, you need to reduce communication between server and client, it’s not possible to completely remove the possibility of latency going up since there needs to be replication, replication is also done automatically by Roblox (Humanoid properties for exemple)

Let’s say you want to create a cool explosion effect with a mesh on input, if I was a beginner, I would go for the simplest way and do the following:

  • Fire a remote event on input
  • Create mesh on server
  • Tween size, color, transparency on server
  • Destroy mesh on server after tweening is complete

I also want to tween at a rate of 1/30th a second.
If I create dozens of explosions with this method, not only will the latency go up but the explosion will also look very ugly, it won’t be smooth at all.

So what’s going on exactly, why wouldn’t this work?

Any visuals done on the server will automaticaly replicate to all clients, any properties changed will also automaticaly replicate.

  • Part is made on the server, replicates to all clients
  • Part have its size, color and transparency changed on the server, replicates to all clients
  • Properties replicate every 1/30th a second

The problem here is the constant communication between server and client, this exemple here will cause latency to go up, the replication will be slower and less accurate the more of it there is.

To fix this, you need to make all the clients do their own work independantly, instead of asking the server to do all the work and replicate the work to all clients, make all the clients create the part and do the tweening.

  • Fire a remote event on input
  • Use the FireAllClients method to fire the remote event to all clients
  • Create mesh on clients
  • Tween size, color, transparency on clients
  • Destroy mesh on clients after tweening is complete

The only communication is the input from the client and the server telling all the clients to do something on their own, no more replication is done just for an explosion, if you wanted more explosions in a row, you can also create those multiple explosions on each clients by still firing the remote event for one, it depends on how you want to do it.

Now what if we wanted to do something more complex and create a projectile?
You can handle everything related to visuals and leave the damage, experience to the server, there’s multiple ways to create projectiles, you can create the projectile on the server and do the effects on the clients while using a Touched event on the projectile on the client and the server so they do their job seperately and in a secured manner, or you can leave the visuals and the hit detection on the client and fire a remote to the server and handle data there, you would need a way to sanitarize the inputed data.

Never allowing the server to see any visuals and making the clients do the visual work (sounds as well) is called simulation replication.

Here is a thread on how to use simulation replication: How to use the Simulation Replication method

78 Likes

This is great! I think you talked about mostly everything that can cause lag, espacially making the client do stuff that it should do instead of the server.

4 Likes

@SteadyOn wrote a module for replicating TweenService that is honestly a godsend. If anyone’s looking for a fast and easy way to replicate TweenService properly, it’s a good resource.

8 Likes

When writing code fast, I tend to gloss over what I’m doing on the server and client, only aiming for results. However, whenever I realized this, I wasn’t quite sure what the most efficient method was to get the same job done. Very nice post, clarified this question that I’ve had for quite some time. Will make sure to have a look at my current scripts and update them.

2 Likes

Can you present an example game of this projectile client replication?

EDIT: Nevermind! I found your thread on an example!

2 Likes

Added the thread to this post, should’ve done that some time ago lol

1 Like