question

rkacalski avatar image
rkacalski asked

Edit and Update Player Data in Cloud Script

I'm working on creating a prize table for my new leaderboard. I want to reward the player with an item that is stored in a JSON in player data (title) via cloud script. I want to get the index of that item and increase it in the script. 5603-data.png

I was trying to accomplish this by getting the players data. Updating it at the index and then updating. However this doesn't seem to be working. Any help would be greatly appreciated!

 handlers.dailyRewardPos1 = function (args, context) 
 {
     var getPlayerInfo = server.GetUserData
         ({
             PlayFabId: currentPlayerId,
             Keys: ["PlayerData"]
         });
            
     var myObject = JSON.parse(myJsonString);
     var itemToUpdate = myObject.PlayerData[22] += 1;
            
     var Update = server.UpdateUserData
         ({
             "PlayFabId": currentPlayerId,
             "Data": itemToUpdate
         });
            
 }
Player DataCloudScriptLeaderboards and Statistics
data.png (11.3 KiB)
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

·
Simon Cui avatar image
Simon Cui answered

I did a test and modified your code to make it worked as shown below. Besides, the yellow highlight in the image data.png is not the place of index 22. The main issue is in var itemToUpdate = parseInt(myObject.PlayerData[22]) + 1;. You can refer to the document: Player Data Management - Get User Data - REST API (PlayFab Server) | Microsoft Learn to get the right data structure; This tutorial can also help to debug your codes: Writing custom CloudScript (Legacy) - PlayFab | Microsoft Learn

 handlers.dailyRewardPos1 = function (args, context) 
 {
     var getPlayerInfo = server.GetUserData
         ({
             PlayFabId: currentPlayerId,
             Keys: ["PlayerData"]
         });
        
     // get original value 
     var myObject = getPlayerInfo.Data.PlayerData.Value;
        
     // convert to array
     var newObject = JSON.parse(myObject);
    
     log.info(newObject);
     // specify which index is
     var index = 22;
    
     // increase the number of index item by 1
     newObject[index] = newObject[index] + 1;
    
     // convert array to Json
     updatedJSONData = JSON.stringify(newObject);
     log.info(updatedJSONData);
        
     var Update = server.UpdateUserData
         ({
             "PlayFabId": currentPlayerId,
             "Data": {
                 "PlayerData": updatedJSONData
             } 
         });
            
 }
1 comment
10 |1200

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

rkacalski avatar image rkacalski commented ·

Thanks so much!

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.