question

Opticgamer avatar image
Opticgamer asked

Can't get player displayname or username from PlayerLeaderboardEntry

Hi, I'm trying to make a leaderboard but I'm running into some issues when I try to get the players score and name for the leaderboard listings It always says that "DisplayName" nor "Username" exist in PlayerLeaderboardEntry but PlayFabId works. I want to grab the players username for the leaderboard entry but it just wont let me. Putting "LL.playerNameText.text = player.PlayFabID;" works fine but "LL.playerNameText.text = player.Username;" or "LL.playerNameText.text = player.DisplayName;" doesn't.
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

using PlayFab;

using PlayFab.ClientModels;

using PlayFab.ProfilesModels;



public class LobbyLeaderboard : MonoBehaviour

{

    public GameObject LBUI;

    public GameObject ListingPrefab;

    public Transform ListingContainer;



    private bool Show = false;



    // Start is called before the first frame update

    void Start()

    {

        LBUI.SetActive(false); 

    }



    public void ShowMenu()

    {

        Show = !Show;

        if (Show)

        {

            GetLeaderboard ();

            LBUI.SetActive(true);

        }

        else

        {

            LBUI.SetActive(false);

            DestroyLeaderboard ();

        }

    }



    public void DestroyLeaderboard()

    {

        for(int i = ListingContainer.childCount - 1; i >= 0; i--)

        {

            Destroy (ListingContainer.GetChild (i).gameObject);

        }

    }



    public void GetLeaderboard()

    {

        var requestLeaderboard = new GetLeaderboardRequest { StartPosition = 0, StatisticName = "TotalGames", MaxResultsCount = 20 };

        PlayFabClientAPI.GetLeaderboard (requestLeaderboard, OnGetLeaderboard, OnErrorLeaderboard);

    }



    public void OnGetLeaderboard(GetLeaderboardResult result)

    {

        //Debug.Log(result.Leaderboard[0].StatValue);

        foreach (PlayerLeaderboardEntry player in result.Leaderboard) 

        {

            GameObject tempListing = Instantiate (ListingPrefab, ListingContainer);

            LeaderboardListing LL = tempListing.GetComponent<LeaderboardListing> ();

            LL.playerNameText.text = player.Username;

            LL.playerScoreText.text = player.StatValue.ToString();

            Debug.Log(player.DisplayName + ": " + player.StatValue);

        }

    }



    public void OnErrorLeaderboard(PlayFabError error)

    {

        Debug.LogError (error.GenerateErrorReport ());

    }



}

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.

1 Answer

·
Ivan Cai avatar image
Ivan Cai answered

You can navigate to client profile options in the title settings to see whether the DisplayName option is checked. If not, please check it. Next, you can set ProfileConstraints property to get different client profile contents. For more detailed information about profile constraints, please refer to https://docs.microsoft.com/en-us/rest/api/playfab/client/player-data-management/getleaderboard?view=playfab-rest#playerprofileviewconstraints.

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.