Course contentsShow
← Unreal Engine Game Development
Lesson 119 of 8802. The Gameplay FrameworkFree lesson

Implementing Server-Authoritative Scoring

You will write logic that updates player scores on the server to prevent cheating and ensure consistency across the network.

In this lesson, you will learn how to secure your game’s scoring system by shifting the authority of score updates from the client to the server.

The Problem with Client-Side Trust

In multiplayer games, you might be tempted to let a player’s local machine tell the server, "I just scored 10 points." However, a malicious player could easily modify their local game code to send that message every frame, granting themselves an infinite score. In networking, we never trust the client. Instead, we use Server-Authoritative Scoring.

The Flow of Authority

In this architecture, the client only acts as a messenger. When a player performs an action that should grant points (like hitting a target), the client sends an RPC (Remote Procedure Call) to the server saying, "I think I hit a target."

The server then validates this claim—checking the player's position, the target's status, and the rules of the game. If the server confirms the action is legitimate, it updates the score variable on the server side. Because that variable is marked for Replication, the server automatically pushes the updated score back down to all clients.

Implementation Snippet

In your PlayerState or GameState class, you define a score variable and a function to update it. By using Reliable RPCs, you ensure the server receives the request:

// On the Server
void AMyGameState::AddScore(APlayerState* Scorer, int32 Amount)
{
    if (HasAuthority())
    {
        // Add the score only on the server
        Scorer->SetScore(Scorer->GetScore() + Amount);
    }
}

The client’s local UI simply listens for the replicated variable to change. When the server updates the score, the OnRep_Score function triggers on the clients, ensuring their screens reflect the official, server-verified tally. By centralizing the math on the server, you ensure that no matter how much a player tries to manipulate their local memory, the server remains the ultimate source of truth for the game leaderboard.

Key Takeaway: Always calculate and update scores on the server, then rely on variable replication to keep all clients synchronized with the authoritative state.