question

Kim Strasser avatar image
Kim Strasser asked

CloudScript: How can I found out if a player statistic exists?

I get an error message when I want to get the value of the statistic "Playeravatar". In this case, the player doesn't have a value in the leaderboard "Playeravatar".

var avatarnumber = 0;
var result = server.GetPlayerStatistics({PlayFabId: currentPlayerId, StatisticNames: "Playeravatar"});
if (result.error != null)
{

}
else
{
    if ((result.Statistics[0].Value != null) && (result.Statistics[0].Value != ""))
    {
        avatarnumber = result.Statistics[0].Value;
        
    }
}

return { avatarnumber };
"Error": {
            "Error": "JavascriptException",
            "Message": "JavascriptException",
            "StackTrace": "TypeError: Cannot read property 'Value' of undefined\n    at handlers.GetPlayerAvatar (E5E2C-main.js:4168:35)\n    at Object.invokeFunction (Script:116:33)"
        }

How can I find out in CloudScript if the player already has a value for the statistic "Playeravatar"?

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

·
Seth Du avatar image
Seth Du answered

Simply check existing should do the job, for example:=

    if (result.Statistics[0]==null)
    {
        return ("no value");
    }
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.