Where to put code, Server or Client?

Hi, I am looking to understand when to add code server side or client side.

For most features, the answer is clear, but in some situations I get a bit torn.

For example, let’s say we want to make an enemy in a game. The enemy just walks back and forth. When the player touches the enemy, the player is stunned. When the player jumps on top of the enemy, the enemy dies and the player is rewarded a coin.

The resources used are a BodyVelocity updating the movement of the enemy. Particle effect played on player when stunned as well as stopping players movement and jump. When enemy is killed it also plays an effect as well as flashes its parts from opaque to transparent for a second, rewards the player, and then is deleted from the game.

Now implementing this server side initially makes sense to me. You have an enemy that everyone needs to see. It will be moving back and forth, so you would want to be sure that its’ position is the same on everyone’s machine. Rewarding the player server side instead of client side is safer.

But, I have read many posts regarding keeping the server code light. So this leads me to question, is doing it on the server the correct way? If not, I have a hard time understanding how parts of this could be implemented client side, such as moving the enemy (since if one clients code is moving the enemy, then all clients are moving the enemy).

Overall, I think I am looking for a set of guidelines I can checkoff when implementing a feature that help me determine the best place to put my code.

Thank you all for your help

Well, for the enemies, I would probably put the code on the server side. As for the effects on the player that interacts with the enemy, I would probably put that code on the client.

1 Like

Enemy movement will have to be server sided if you want it to coexist on the server and all clients.

As for particle effects, you can do either client or server side. Server side is much easier to implement, however, but client side is much more fluid.

When it comes to the question of ‘Should I make the code server or client sided?’, you need to ask yourself this: is it necessary to provide better user experience? In this sense, I’m talking about how fast certain aspects of a game will react to what the client does. As evident with server-sided code, there is a barrier that must be crossed when replicating and transmitting information to the clients in the game. There will be latency, so whether you think that latency will ruin some immersion for the player, that will be the determining factor if the code should be client-side or server-side. Sometimes it isn’t a black and white picture for this sort of stuff; it’s dependent on what’s more important. However, there’s some given things to do on the server, such as enemy movement as you were asking in your question. In the end, it’s up to you to decide the priority of where code belongs.

Another note: sometimes providing better user experience can lead to more exploitable actions from clients, so be wary of such and implement sanity checks wherever possible.


Thanks for your response. This makes sense.
I have a separate, but related question. I have read that you should try to do tweening client side, largely because of performance issues, but as you mentioned above, I can see a better user experience benefit from it as well.

In the case that you have a door in your game that you tween open, is this another situation in which server is your only good choice because this is a door everyone needs to see open? Or is it possible to do this client side?

For instance: you have a door on the server, player clicks on the door which sends an event to the server. Server sends an event to all players that the door has been opened. When clients receive the event, they all tween their instance of the door. A new player joins, but this player wasn’t in the game when everyone got the open door event, so his door is closed.

These edge cases can be fixed, but it seems like a lot of work for possibly little gain.