Scoreboard/ leaderBoard

help, my code only works on the sever but never on the client, that is only the text, the scores list dont update for anyone.

[ClientRpc]
    void RecordScoreClientRpc()
    {
        if (!IsServer) return;
        foreach (ulong clientID in NetworkManager.Singleton.ConnectedClientsIds)
        {
            var player = NetworkManager.Singleton.ConnectedClients[clientID].PlayerObject.GetComponent<MyHealth>();
            scores.Add(player.kills.Value);
            this.dispScores.text += Environment.NewLine + player.kills.Value;
        }
        print(killScore.text);
    }

i called the above function in the serverrpc below which execute each time playertakes damage

[ServerRpc(RequireOwnership = false)]
    public void TakeDamageServerRpc(int damage, byte clientID, byte myID)
    {
        hitMan = NetworkManager.Singleton.ConnectedClients[myID].PlayerObject.GetComponent<MyHealth>();
        playerhit = NetworkManager.Singleton.ConnectedClients[clientID].PlayerObject.GetComponent<MyHealth>();

        if (networkHealth.Value > 0)
            if (IsServer) playerhit.networkHealth.Value -= damage;
        if (networkHealth.Value <= 0)
        {
            //GameOver();
            isDead = true;
            DieClientRpc();
            if (IsServer) hitMan.kills.Value++;
        }
        RecordScoreClientRpc();
    }

Hi @liambilly ,
I you want to run the “RecordScoreClientRpc()” on the clients, you shouldn’t add “if (!IsServer) return;” line on top of the method.

I think, you couldn’t fully understand the RPC methods working logic.
Because “RecordScoreClientRpc()” just run on the clients (also Host player).
And just Host/Server reaches the “NetworkManager.Singleton.ConnectedClients” list!
So if you remove the line of “if (!IsServer) return;” on the method, that will give an error!

Good luck.

yes it gives an error when i remove it, do you any solution to achieve a scoreboard or you know a reference to a project that implements it to see how they do it

For you to understand the RPC and Host - Client structure, I really recommend this video link

aight thanks, lemme check it out n try to apply

1 Like

found a solution by getting an array of players for each player locally