question

amirasfour03@gmail.com avatar image
amirasfour03@gmail.com asked

UpdateUserReadOnlyData with parameter

Hi everyone,

can someone tell me why this does not work:

 var data = args.parameter;
 server.UpdateUserReadOnlyData({ PlayFabId: currentPlayerId, Data: { data : newLevel.toString()} })  

It just creates a new field called data but i want it to update the level i pass.

CloudScript
10 |1200

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

Xiao Zha avatar image
Xiao Zha answered

As the JS documentation says, you need put the variable in brackets “[]” when it used as a key. So, your code should be like this: server.UpdateUserReadOnlyData({ PlayFabId: currentPlayerId, Data: {[data]: newLevel.toString()}}).

10 |1200

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

amirasfour03@gmail.com avatar image
amirasfour03@gmail.com answered

The whole function looks like this: handlers.IncrementStat = function (args, context) {

         var data = args.parameter;
         var data2 = args.parameter2;
            
         var playerData = server.GetUserReadOnlyData
             ({
                 PlayFabId: currentPlayerId,
                 Keys: [data]
             });
                
         var playerData2 = server.GetUserReadOnlyData
             ({
                 PlayFabId: currentPlayerId,
                 Keys: [data2]
             });
            
         if(playerData && playerData.Data && playerData.Data[data])                                                    
         {
                
             var newLevel = Number(playerData.Data[data].Value) + 1;
             var newLevel2 = Number(playerData2.Data[data2].Value) + 1;
             if(newLevel<=5){
             server.UpdateUserReadOnlyData({ PlayFabId: currentPlayerId, Data: { data2 : newLevel.toString()} })  
             }
             else if(newLevel>=6)
             {
             server.UpdateUserReadOnlyData({ PlayFabId: currentPlayerId, Data: { data : newLevel2.toString()} }) 
             server.UpdateUserReadOnlyData({ PlayFabId: currentPlayerId, Data: { data2 : "0"} }) 
             }
                
         }
         
 };
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.