question

brendan avatar image
brendan asked

Using statistics in Cloud Script

For a call like this, how do I get the value of the statistic in Cloud Script?

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

 

 

10 |1200

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

brendan avatar image
brendan answered

I'm updating the basic Cloud Script all titles get as revision 1 (https://github.com/PlayFab/CloudScriptSamples/tree/master/BasicSample) to update it to use GetPlayerStatistics and UpdatePlayerStatistics, so you'll be able to see it in the sample shortly, but because we've wrapped up the statistics as objects with StatisticName keys, the way to manage stats would now be like this (using the snippet from the basic Cloud Script sample where we're updating the player moves):

 var playerStats = server.GetPlayerStatistics({ 
PlayFabId: currentPlayerId
}).Statistics;

var moves = { StatisticName: "movesMade", Value: 1 };
for (i = 0; i < playerStats.length; ++i) {
if (playerStats[i].StatisticName === "movesMade") {
moves.Value = playerStats[i].Value + 1;
}
}

server.UpdatePlayerStatistics({
PlayFabId: currentPlayerId,
Statistics: [moves]
});
10 |1200

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

Niclas Åberg avatar image
Niclas Åberg answered

How would I get the statistics for just one entry? This is the Cloud-Script code I'm using:

        var request = {
            PlayFabId: currentPlayerId,
            Statistics: [{
                StatisticNames: ["hash"],
            }]
        };
        var result = server.GetPlayerStatistics(request);

The above code returns all entries, as if the StatisticNames variable was omitted:

{ --[[0x126a303f0]]
  HttpRequestsIssued = 0,
  FunctionResult = { --[[0x126a2bcd0]]
    Statistics = { --[[0x126a2be00]]
      1 = { --[[0x126a2be50]]
        StatisticName = "L77U33UDIXBMFN",
        Version = 2,
        Value = 1585411447
      },
      2 = { --[[0x126a2c010]]
        StatisticName = "R99UXXU0I9SXS8I9",
        Version = 0,
        Value = 1585412338
      },
      3 = { --[[0x126a2c100]]
        StatisticName = "hash",
        Version = 0,
        Value = 1585412983
      }
    },
    PlayFabId = "8269FF77D45B5E31"
  },
  FunctionName = "setDiscoveryTime",
  Revision = 71,
  Logs = { } --[[0x1269bbbd0]],
  ExecutionTimeSeconds = 0.0792197,
  APIRequestsIssued = 2,
  ProcessorTimeSeconds = 0.016,
  MemoryConsumedBytes = 87128
}
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.