question

sambracey87 avatar image
sambracey87 asked

Score not sending to Leaderboard

I'm new to Playfab and using Unity. I'm having trouble getting my score to get sent to my Leaderboard. The player connects and logs in fine but I get no information to the score. These are my two scripts.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using PlayFab;
 using PlayFab.ClientModels;
    
 public class PlayfabManager : MonoBehaviour
 {
     // Start is called before the first frame update
     void Start()
     {
         Login();
     }
    
        
     void Login()
     {
         var request = new LoginWithCustomIDRequest
         {
             CustomId = SystemInfo.deviceUniqueIdentifier,
             CreateAccount = true
         };
         PlayFabClientAPI.LoginWithCustomID(request, OnSuccess, OnError);
     }
    
     void OnSuccess(LoginResult result)
     {
         Debug.Log("Successful login/account create!");
     }
    
     void OnError(PlayFabError error)
     {
         Debug.Log("Error while logging in/creating account!");
         Debug.Log(error.GenerateErrorReport());
     }
    
     public void SendLeaderboard(int score)
     {
            
         var request = new UpdatePlayerStatisticsRequest
         {
             Statistics = new List<StatisticUpdate>
             {
                 new StatisticUpdate
                 {
                     StatisticName = "HighScores",
                     Value = score
                 }
             }
         };
         PlayFabClientAPI.UpdatePlayerStatistics(request, OnLeaderboardUpdate, OnError);
     }
    
     void OnLeaderboardUpdate(UpdatePlayerStatisticsResult result) 
     {
         Debug.Log("Succesful leaderboard sent");
            
            
     }
 }

and...

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
    
    
    
    
 public class CollectableControl : MonoBehaviour
 {
     public static int scoreCount;
     public static int highScoreCount;
     public GameObject scoreCountDisplay;
     public GameObject highScoreDisplay;
     public PlayfabManager playfabManager;
        
    
     void Start()
     {
         highScoreCount = PlayerPrefs.GetInt("highScoreCount", 0);
         SceneManager.activeSceneChanged += OnSceneChanged;
     }
    
     void OnSceneChanged(Scene current, Scene next)
     {
         scoreCount = 0;
     }
        
     void Update()
     {
            
         scoreCountDisplay.GetComponent<TMPro.TextMeshProUGUI>().text = "" + scoreCount;
         highScoreDisplay.GetComponent<TMPro.TextMeshProUGUI>().text = "" + highScoreCount;
    
         if (scoreCount > highScoreCount)
         {
             highScoreCount = scoreCount;
             PlayerPrefs.SetInt("highScoreCount", highScoreCount);
         }
     }
    
     public void HighScores()
     {
         playfabManager.SendLeaderboard(highScoreCount);
            
     }
        
    
    
 }
Leaderboards and Statistics
13 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.

Simon Cui avatar image Simon Cui commented ·

Hi, I tested your first-part codes of Class PlayfabManager and it updated statistics to a player successfully when I passed a score. May I know did you get any specific error message? If so, what line of codes generated this error when debugging? Besides, how did you check the score information? Is it through player statistics in Game Manager or PlayStream Monitor? I suggest you debug your codes again especially focusing on the class CollectableControl.

1 Like 1 ·
sambracey87 avatar image sambracey87 Simon Cui commented ·

Thanks for the response.

 public void SendLeaderboard(int score)
      {
                
          var request = new UpdatePlayerStatisticsRequest
          {
              Statistics = new List<StatisticUpdate>
              {
                  new StatisticUpdate
                  {
                      StatisticName = "HighScores",
                      Value = score
                  }
              }
          };
          PlayFabClientAPI.UpdatePlayerStatistics(request, OnLeaderboardUpdate, OnError);
      }

The SendLeaderboard function doesn't seem to be running at all so when I attach a Debug.Log() nothing shows up.

0 Likes 0 ·
Simon Cui avatar image Simon Cui sambracey87 commented ·
 public void SendLeaderboard()
       {
                    
           var request = new UpdatePlayerStatisticsRequest
           {
               Statistics = new List<StatisticUpdate>
               {
                   new StatisticUpdate
                   {
                       StatisticName = "HighScores",
                       Value = 10
                   }
               }
           };
           PlayFabClientAPI.UpdatePlayerStatistics(request, OnLeaderboardUpdate, OnError);
       }

You may set a fixed number like 10 for testing. I tried the code as shown above and it worked.

1 Like 1 ·
Show more comments

0 Answers

·

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.