Combined score only working for client not server

Hello there!

I’m working on a simple scoring function for a co-op game. When players overlap a target, it adds 5000 to their personal score. What I’m looking to do is have the combined personal score for each player be summed up into an overall score on the scoreboard menu.

Currently, the personal score updates in the UI just fine. It shows 5000 per target consumed. It casts to the PlayerState, gets their personal score, and adds 5000, then casts to the players UI to update the text in the menu. Then I have a run on server event that casts to the GameState and runs a Calculate Overall Score function that loops through the player array, gets their PlayerState and Personal score and adds them together. However, this only works when the client overlaps the target, not the server. And will only update the variable on the server when the client interacts.

This is the code I have for the Target:

This is the code I have for the GameState:

This is the code I have for the UI:

I hope I’ve provided all the necessary information. Any help would be greatly appreciated!

(post deleted by author)

Overlap executes on both client & server, keep that in mind.
So the run-on-server event is useless, code is already running on server. Use an Authority switch instead.

In GameState CalculateOverallScore, you forgot to sum up the scores, you are simply looping through them :

To sum, set variable to 0 before the loop, then in the loop keep adding to the variable.

Hey thank for your reply! I was able to get the overall score working with what you suggested. The last thing that wasn’t working for me was updating the UI to reflect that score on both the client and the server.

The updated GameState looks like this:

And the code I have for the scoreboard UI that runs after the overlap is triggered is this:

Thanks so much for your help! I’m a bit new to the multiplayer side of UE5. Wasn’t sure if the custom event in the score menu needs to run on server or something, I had tried but nothing updated on the client side when the player overlapped a target.

Things like score should always be managed by server, so the code that updates score should only run on server (Authority), then send it to clients - using variable replication is most suited here.

Mark the TotalScore (or OverallScore) variable as replicated with RepNotify. Replicated variables will automatically sync from server to clients (not instantaneous, but within 1sec generally). Enabling RepNotify automatically creates a function OnRep_OverallScore in GameState, which will be automatically called on clients every time the variable receives an update. Use it to update widgets and trigger other events if necessary.

The player score should also be done that way, but if it works keep what you got for now. You’ll probably want to come back to it later once you get a better general understanding of replication.

It worked perfectly thank you so much! I had learned that player scores are managed via PlayerState so I was able to get something put together that worked. Like you said, the further into this project I get I’ll probably figure that what I have currently for player score can be handled better. Thanks again!