How do i send a variable from the client to the server?

When the player falls down and dies, the code is set to print out what player fell. In the picture with the prints, I only fell
down with the client2 3 times. But as you can see it sometimes says 1 instead of 2. Why is this? Is it a glitch or am I doing something wrong?

When I jump down with the server client however, it always says “player 1”

Hello Lufty!

I see some pretty strange stuff going on here.

First of all, you should know that in a multiplayer game, the engine actually spawns a pawn for you on the client and spawns a replicated “you” on the server (You can see this by printing “hello” on BeginPlay and you should see “Server:Hello, Client 1:Hello”). Thus, this code might not be doing what you expect. The variable “PlayersOnServer” might not be updating fast enough for the “set” node on PlayerID be set with “PlayersOnServer = 1”, thus still use “PlayersOnServer = 0”.

Secondly, and what caught my eye the most, what are you expecting to do with “PlayersOnServer” variable? You should know that it actually will not represent the amount of players on the server, as each pawn will have its own instance of this variable (Try playing with 3 players, you should see that the variable will remain = 2). What is going on here is that the pawn server side will call “AddPlayerOnServer” and assign + 1 to the variable, and the RPC on client side will also call the function to assign +1, and that’s it…each pawn will repeat this and end up with “PlayersOnServer = 2”, no matter how many players you have on server…

If you’re looking for to set a playerID to each client and store the amount of players on server, I would recommend you to read more about some important classes on a multiplayer game, like GameMode, GameState and PlayerState to begin with.

There are a lot of ways to know how many players are on a server, but if you wish to store like this, you should use GameMode class (as it only exists on the server) and you can override some functions, like the “PostLogin” or “ChoosePlayerStart” functions that are called when a player joins the server, thus keeping track of how many players actually joins it!

For the playerID you should use PlayerState! Which is where you store scores, kills, flags from each player (and is replicated to everyone). Seems a good class to store it, for me!

Hope this helps, Cheers!

Hi Lufty

I suggest you store the “PlayersOnServer” variable somewhere more appropriate like the Game Mode. When you create a pawn, you are instantiating the class. The new instance will not have the information of other pawn instances in the world.

If you use the GameState class, you can access the “players” array which will tell you exactly how many players are on the server. From the player’s array, you can access the player’s PlayerState class which is used to store information such as the player’s name, their score, their health, etc.

Also the PlayersOnServer variable is set to replicate, when BeginPlay is called, the data might not yet be replicated from the server. So you are setting it on the client, then pushing it to the server overwriting what the server had previously.

Hope this helps.

Alex

Thanks a lot! I didn’t know that I was so far off. I’ve only watched a few videos of replication and thought that most of the logic could be done in the player bp. I will for sure start using the GameMode class instead.
I actually used the PlayerState a few moments after I uploaded this and it worked much better!! Thank you for your help!

I’ll look into the GameState class more! I didn’t really think about that at all when doing this. Thanks a lot for explaining!