question

Ryan Janse van Rensburg avatar image
Ryan Janse van Rensburg asked

Using the documentations player statistics example in a java-script project.

Hi,

I'm having trouble implementing one of the examples for setting payer statistics in java-script.

Coming from a c# background and know very little about java-script. I'm trying to send score information to a PlayFab leaderboard. Below is the example I'm trying to convert. Any help would be greatly appreciated.

PlayFabClientAPI.UpdatePlayerStatistics( new UpdatePlayerStatisticsRequest {
    // request.Statistics is a list, so multiple StatisticUpdate objects can be defined if required.
    Statistics = new List<StatisticUpdate> {
        new StatisticUpdate { StatisticName = "strength", Value = 18 },
    }
},
result => { Debug.Log("User statistics updated"); },
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

·
Sarah Zhang avatar image
Sarah Zhang answered

Your code should be the following Js format. If you don't know much about Js. Please check our Js SDK Page first. If you click the [Download Source Code] on this page. You can navigate to the Js SDK Github page. There are SDK and some sample projects on it. You can also check the QuickStart for a simple tutorial.

 function UpdateStats() {
    var updateStatsRequest = {
        Statistics: [{ StatisticName: "[Your Stat Name]", Value: [Your int value] }]
            };
    PlayFabClientSDK.UpdatePlayerStatistics(updateStatsRequest, updateStatsCallback);
        }
 var updateStatsCallback = function (result, error) {
    if (result !== null) {
        document.getElementById("resultOutput").innerHTML = "You made your first successful API call!";
            } else if (error !== null) {
        document.getElementById("resultOutput").innerHTML =
                    "Something went wrong with your first API call.\n" +
                    "Here's some debug information:\n" +
                    CompileErrorReport(error);
            }
        };

  function CompileErrorReport(error) {
     if (error === null)
        return "";
     var fullErrors = error.errorMessage;
     for (var paramName in error.errorDetails)
        for (var msgIdx in error.errorDetails[paramName])
             fullErrors += "\n" + paramName + ": " + error.errorDetails[paramName][msgIdx];
     return fullErrors;
        }
1 comment
10 |1200

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

Ryan Janse van Rensburg avatar image Ryan Janse van Rensburg commented ·

Thank you! This worked perfectly

0 Likes 0 ·

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.