question

steelfox001 avatar image
steelfox001 asked

CloudScript GetPlayerStatistics TypeError: Cannot read property 'favorit' of undefined

In Unity i get error. TypeError: Cannot read property 'favorit' of undefined

Where i made mistake?

var playerStatisticsFavorit = server.GetPlayerStatistics({

"PlayFabId": currentPlayerId,

"Keys": ["favorit"] });

var favorit = playerStatisticsFavorit.Data["favorit"];

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

·
brendan avatar image
brendan answered

If you have a look at the example response for https://api.playfab.com/documentation/Client/method/GetPlayerStatistics, you'll see that in the Data of the response is "Statistics", which is the array of all the stats being returned. Each element of the array is a JSON object containing "StatisticName", "Value", and "Version". So what you really want is the "Value" element of the 0 member of the array Statistics in the Data, or:

.Data.Statistics[0].Value

2 comments
10 |1200

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

steelfox001 avatar image steelfox001 commented ·

Still have problem.

Unity show what playerStatisticsFavorit contain result.FunctionResult{ {"PlayFabId":"330AC16CB43089AD","Statistics":[{"StatisticName":"favorit","Value":0,"Version":0}]}}

and now i want put Value of "favorit" in var favorit, but i get error TypeError: Cannot read property 'Statistics' of undefined

-------------------------------------

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

var favorit = playerStatisticsFavorit.Data.Statistics[0].Value;

0 Likes 0 ·
brendan avatar image brendan steelfox001 commented ·

Here's more generic model - define a <string, int> Dictionary "userStats", and process your results like so:

foreach (var each in playerStatisticsFavorit.Statistics)
    userStats[each.StatisticName] = each.Value;

That'll add all the stats returned by your call to the dictionary, and you can then look them up from it.

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.