question

reubencovington avatar image
reubencovington asked

Reading Statistic Values

I'm trying to retrieve player statistic data (xp) from the server side using the GetPlayerStatisticsRequest() but when I go to access the list, the item doesn't seem to exist. Here my code:

function GetPlayerXp(args) {

	var statisticRequest = server.GetPlayerStatistics({
        	PlayFabId: currentPlayerId,
        	StatisticNames: [ "xp" ]
        });

	
	var xp = 0;
	for (var statistic in statisticRequest.Statistics) {
        	// When going into the loop here, 'statistic' just equals "0"
		if (statistic.StatisticName == "xp") {
            		xp = statistic.Value;
        }

	return xp;
}

Now, I know that the request is correct, and it gives me the correct statistics list as shown when I write a title event:

But my problem is that when it goes into the 'for' loop for the list, the 'statistic' variable just equals "0". It's not a StatisticValue type of variable.

How can I access the statistic value from that list (statisticRequest.Statistics) ?

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

·
Sarah Zhang avatar image
Sarah Zhang answered

In JavaScript, the parameter in the “for” loop which before “in” should be the index of an array, not an item of it. So you can refer to the following code to modify it.

handlers.GetPlayerXp = function (args, context) {
    var statisticRequest = server.GetPlayerStatistics({
        PlayFabId: currentPlayerId,
        StatisticNames: ["xp"]
    });

    var statistic = statisticRequest.Statistics;
    var xp = 0;

    for (var key in statistic) {
        if (statistic[key].StatisticName == "xp") {
            xp = statistic[key].Value;
        }
        return xp;
    }
}
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.