Storing/Loading Players HP, Coins, Item Count in multiplayer

Hi! I’m trying to make a game with LAN multiplayer. Currently the problem is that:

  • One player gets a coin, everyone gets that coin.
  • One player gets an item, everyone gets that item.
  • One player gets damage from enemy and loses 1HP, but everyone else dies.

BP_Player is the “ThirdPersonCharacter”.

Problem is not with the “hud” widget, that displays the HP meter and coin/item counts. I’ve checked those multiple times with the “print string” event.

I’ve tried to keep values inside the Gamemode, PlayerState and PlayerController, but there is no difference at all.

Each time I see tutorials, where they store their HP, Coins and other stuff, all this happens inside the “ThirdPersonCharacter”, but I don’t want that, because in my game character posses other characters, and in the future player can swap character on the fly.

1 Like

Sounds about right for this sort of stuff, assuming HP is character-bound, and if you die you lose collected coins.

Each player has one PlayerController, one PlayerState, and one Character (most of the time, one at a time).
If you want to be able to swap characters and keep the same HP and Coins values, then surely you don’t want to put the variables in Character. Put them in PlayerState or PlayerController instead.

I believe the main issue with your stuff comes from various places where :

  • You are executing important gameplay logic on client instead of resticting it to server (authority). For example, overlap events trigger on both server and clients. When an enemy overlaps Player1, then the overlap event will also trigger on Player2 and Player3’s machines! Same when another player is overlapping a coin. So you want to gate your code so that important stuff only happens on the Authority (server) machine. Use a “Switch Has Authority” node.

  • You are using nodes “GetPlayerController(0)” or “GetPlayerCharacter(0)” on server code. These nodes will access the first available player on the machine, which on server doesn’t really mean anything contextually. Those nodes are fine to use on client for eg. UI code, but not on server. On server you need to use context to modify the appropriate player. For example Overlap event provides “Other Actor” which you cast to BP_PLAYER. But instead of using the playercontroller related to that player, you are currently using “GetPlayerController(0)”. This is wrong, use BP_PLAYER->GetController instead. Same for coin pickup.

Also make sure your variables (COIN, HP) are set to replicate. They don’t seem to be atm. This will ensure once the Authority machine has updated those variables, their value will be synchronized to client(s).

2 Likes

Thank you! Now it works very well.