question

celestinelim2003 avatar image
celestinelim2003 asked

Leaderboard always with Value 0

I really new to Playfab and followed the code I found but I dont know why the value is always 0. The score is updated but the value is still 0. The score is added in another script, is it OK?

Also, I received the error messages:

Failed to update leaderboard: HTTP/1.1 409 Conflict UnityEngine.Debug:LogError (object)

Failed to update leaderboard: The client has exceeded the maximum API request rate and is being throttled

 public IEnumerator SendLeaderboardCoroutine(int score)
     {
         var request = new UpdatePlayerStatisticsRequest
         {
             Statistics = new List<StatisticUpdate>
             {
                 new StatisticUpdate
                 {
                     StatisticName = "Platform Score",
                     Value = score
                 }
             }
         };
    
         PlayFabClientAPI.UpdatePlayerStatistics(request, result =>
         {
             if (result != null)
             {
                 Debug.Log("Leaderboard updated successfully!");
             }
             else
             {
                 Debug.LogError("Failed to update leaderboard: result is null.");
             }
         }, error =>
         {
             Debug.LogError("Failed to update leaderboard: " + error.ErrorMessage);
         });
    
         // Wait for a short delay after the PlayFab API call
         yield return new WaitForSeconds(1f);
     }
Leaderboards and Statistics
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Xiao Zha avatar image
Xiao Zha answered

How you find the score is updated but the value is still 0? I have tested the code you provided, If the Score parameter be passed correctly, it can update the statistic successfully and I can get the score via GetLeaderboard API.

The “HTTP/1.1 409 Conflict” error indicates that there were concurrent attempts to update the same statistic at the same time and that the second update was rejected thus creating 409 conflict. And the "Client has exceeded the maximum API request rate and is being throttled" error message means that you are calling the API too frequently and exceeding this API request rate limit. You may reduce the frequency of API calls to avoid this error.

In addition, we recommend using the Server API to update statistics on Cloud Script(https://learn.microsoft.com/en-us/gaming/playfab/features/automation/cloudscript-af/quickstart), because updating statistics on the client may allow cheaters and hackers to freely modify statistics and cause damage to your game.

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

celestinelim2003 avatar image celestinelim2003 commented ·

I have a score manager script which I also added a text to allow the score to be seen during the game. This text shows the score is updated however when I check the leaderboard, the value is still 0. Hence, I am not sure if the score parameter is passed correctly.

How do I make sure the score parameter for the leaderboard value is the same as the score the player gets during the game? I am using Unity so do I need to make sure the scripts are in the same GameObject?

Thank you.

0 Likes 0 ·
Xiao Zha avatar image Xiao Zha commented ·

Different Scripts is fine, just make sure the score passed in your SendLeaderboardCoroutine method is correctly, you cloud debug the score in this method to see if the score passed correctly.

You may also check the statistic in GameManager like below to see if the Statistics updated correctly. 6888-playerstatistics.png

Or check the leaderboard section.

6889-leaderboardstatistics.png

0 Likes 0 ·
celestinelim2003 avatar image celestinelim2003 commented ·

I checked but the value in the leaderboard section and in GameManager is 0, even though the score.Text is updated. Not sure what went wrong? Also, I still received such error, not sure how to avoid it? Thank you.

 Failed to update leaderboard: HTTP/1.1 409 Conflict
 UnityEngine.Debug:LogError (object)
 PlayfabManager/<>c:<SendLeaderboardCoroutine>b__20_1 (PlayFab.PlayFabError) (at Assets/Scripts/PlayfabManager.cs:174)
 PlayFab.Internal.PlayFabUnityHttp:OnError (string,PlayFab.Internal.CallRequestContainer) (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:237)
 PlayFab.Internal.PlayFabUnityHttp/<Post>d__12:MoveNext () (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:151)
 UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)
0 Likes 0 ·
Xiao Zha avatar image
Xiao Zha answered

You can try calling your SendLeaderboardCoroutine method directly to rule out the code issues. If calling the method directly still fails to update the statistics correctly. Please provide your Title ID for our research. This is a code example that calls your method directly:

 using PlayFab;
 using PlayFab.ClientModels;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
    
 public class UpdateStatisticTest : MonoBehaviour
 {
     // Start is called before the first frame update
     void Start()
     {
         PlayFabClientAPI.LoginWithCustomID(new LoginWithCustomIDRequest
         {
             CustomId = "test"
         }, re =>
         {
             Debug.Log("Login Success");
                
             StartCoroutine(SendLeaderboardCoroutine(10));
            
         }, er =>
         {
             Debug.Log("Failure" + er.GenerateErrorReport());
         });
     }
    
    
     public IEnumerator SendLeaderboardCoroutine(int score)
     {
         Debug.Log(score);
            
         var request = new UpdatePlayerStatisticsRequest
         {
             Statistics = new List<StatisticUpdate>
             {
                 new StatisticUpdate
                 {
                 StatisticName = "Platform Score",
                 Value = score
                 }
             }
         };
    
         PlayFabClientAPI.UpdatePlayerStatistics(request, result =>
         {
             if (result != null)
             {
                 Debug.Log("updated successfully!");
             }
             else
             {
                 Debug.LogError("Failed to update leaderboard: result is null.");
             }
         }, error =>
         {
             Debug.LogError("Failed to update leaderboard: " + error.ErrorMessage);
         });
    
         // Wait for a short delay after the PlayFab API call
         yield return new WaitForSeconds(1f);
     }
 }
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.