question

Kim Strasser avatar image
Kim Strasser asked

How can I find out if a player already has a value in a certain leaderboard?

I use server.GetLeaderboardAroundUser in CloudScript to find out if the player already has a score in a certain leaderboard, but the problem is that it returns the value 0 even if the current leaderboard version is completely empty.

"Message": "PlayerPosition: 1 CurrentPlayerScore: 0",           

How can I find out if the player already has a value in the current leaderboard version? I only want to do something if the player has no value in the current leaderboard version.

CloudScript code:

var resultleaderboard = server.GetLeaderboardAroundUser(
{
    PlayFabID: currentPlayerId,
    StatisticName : leaderboardname,
    MaxResultsCount : 1
});

log.info("resultleaderboard.Version: " + resultleaderboard.Version);

if ((resultleaderboard != null) && (resultleaderboard.Error == null))
{
    var CurrentPlayerScore = 0;
    resultleaderboard.Leaderboard.forEach(element => {
    if (element.PlayFabId == currentPlayerId)
    {
        var PlayerPosition = element.Position + 1;
        CurrentPlayerScore = Number(element.StatValue) * -1;
        log.info("PlayerPosition: " + PlayerPosition + " CurrentPlayerScore: " + CurrentPlayerScore);
        return;
    }
    });               
}
CloudScript
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

·
Gosen Gao avatar image
Gosen Gao answered

The statistic value of the user defaults to 0 when the user does not have the corresponding statistic. Most games won’t update 0 as a new value of statistic, so you will know the player doesn’t have statistic in this Leaderboard if the value is 0.

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.