I use CloudScript to update my leaderboard. But server.GetLeaderboardAroundUser always returns the player's last highscore and not his current highscore.
For example:
1) The client calls the CloudScript the first time after the reset of the leaderboard:
args.Playerscore = 12
element.StatValue.toString() = 0
2) The client calls the CloudScript the second time:
args.Playerscore = 16
element.StatValue.toString() = 12
3) The client calls the CloudScript the third time:
args.Playerscore = 20
element.StatValue.toString() = 16
Why is element.StatValue.toString() not returning the player's current highscore?
handlers.UpdateLeaderboard = function (args, context) { var AddScore = UpdatePlayerScore(args.Leaderboardname, args.Playerscore); var CurrentScore = ""; if (AddScore.leaderboardupdated == true) log.info("User statistics updated."); else log.info("Could not update statistics."); var resultleaderboard = server.GetLeaderboardAroundUser( { PlayFabID: currentPlayerId, StatisticName : args.Leaderboardname, MaxResultsCount : 1, ProfileConstraints: { ShowDisplayName : true } }); if ((resultleaderboard != null) && (resultleaderboard.Error == null)) { resultleaderboard.Leaderboard.forEach(element => { if (element.PlayFabId == currentPlayerId) { var PlayerPosition = element.Position + 1; CurrentScore = PlayerPosition + " " + element.Profile.DisplayName + " : " + element.StatValue.toString(); return; } }); } else CurrentScore = "Something went wrong. Could not get leaderboard highscore."; return CurrentScore; } function UpdatePlayerScore(leaderboardname, score) { var result = server.UpdatePlayerStatistics({ PlayFabId: currentPlayerId, Statistics : [{ StatisticName : leaderboardname, Value : score }] }); if (result.Error == null) return { leaderboardupdated: true }; else return { leaderboardupdated: false }; }