Hud added multiple times in multiplayer (network)

I’m trying to add a health bar HUD to my game. I created a hud widget blueprint and set it all up. When I add it to the game like in most of the samples I’ve come across, it ends up added multiple times in a multiplayer network game.

I’m using my custom Player Controller, Event Begin Play, calling Create Widget, then Add to Viewport.

When I play a 2 player networked game, the Add to Viewport gets called twice for the server and twice for the client. This ends up making a duplicate hud of the wrong player. So Player 1 sees Player 2’s hud overtop theirs. Player 2 sees Player 1’s hud overtop of theirs.

I can’t figure out how to only add the HUD to the local user.

Try this:

1-) Add Get Player Controller node, set its index to 0 and connect it to Owning Player.

2-) Put the script into custom event, set replication to “Run On Client” + Reliable and call the event after BeginPlay for player controller.

Even as I type, I find these methods -logically- unnecessary but, this was how I handled the same issue in past.
Give it a try.

1 Like

That worked great. Thanks for the help.

Glad if I could be useful :slight_smile:
Have a great day.

Btw, I think figured logic behind this. Event BeginPlay fires whenever a new controller is generated. In multiplayer server machine creates controller for every connected player. And there is a player controller created for each client as well (on the client side).
Since clients get one controller (their own), BeginPlay only triggers once for them. For server, however, since it has as many controllers as there are players, his BeginPlay for PlayerController will execute each time a new player joins.

That makes sense.

Necroed, for anyone coming later: Now you can use “Is Local Player Controller” node to prevent server creating multiple instances

1 Like

I added a image to show how it worked for me, I need the REF variable too, just have to remember to set it to Replicated and the isValid because it was creating the widget 3 times, its working well so far. Thank you all!

1 Like