handlers.UpdateTotalRanking = function(args){ var result = server.GetPlayerProfile( {PlayFabId: currentPlayerId, ProfileConstraints: {ShowLocations: true}} ); var profile = result.PlayerProfile; var gameMode = args.gameMode; var totalScore = 0; debug.log(profile.Statistics.Count); var i = 0; for(i;i<profile.Statistics.Count;i++){ var name = profile.Statistics[i].Name; debug.log(name); if(name.includes(gameMode)){ debug.log("TRUE"); totalScore+=profile.Statistics[i].Value; } } if(totalScore>0){ log.info("UPDATETOTALSCORE"); var req = { PlayfabId:currentPlayerId, Statistics : [{ StatisticName : "TotalRank"+gameMode, Value : totalScore }] } return server.UpdatePlayerStatistics(req); } return profile; };
When user statistics are updated, I want to set the sum of other statistics to total ranking. But this code doesn't work. I want your help.
There is no ShowStatistics in the code above, but inserting it will not fix the problem.
Answer by Rick Chen · May 18, 2021 at 08:14 AM
The result image you provide shows that the “APIRequestsIssued” is 0, meaning it didn’t even execute the 2nd line of your code snippet. Are you sure that you have made the correct request? How does this CloudScript get called? Could you please provide your request body of the API call for us to diagnose?
When I tried your code in my title by calling the ExecuteCloudScript API, I received this error: "ReferenceError: debug is not defined\n at handlers.UpdateTotalRanking (XXXX-main.js:39:5)\n at Object.invokeFunction (Script:116:33)". Please note that the debug.log you used in line 6 is not predefined function, you may use the log.info instead.