question

blazej avatar image
blazej asked

Update PlayerData With CloudScript

Hello, would you be able to write a code that will allow one player to send a cloudscript that will add a json data to another player's title key? (name of the key is "gifts") I mean something like this:

{"friendID1":XXXXX,"friendID2":XXXXX}
//want to add friendID3

I would like to add "friendID3" to this data, which will be the Player Title ID of the player sending this cloudscript.

Could you write something like this? Thank you very much in advance for your help, best regards and have a nice day!

Player DataCloudScript
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.

Neils Shi avatar image Neils Shi commented ·

I will do some research.

1 Like 1 ·
blazej avatar image blazej Neils Shi commented ·

Cool! Then I am waiting for some info.

0 Likes 0 ·

1 Answer

·
Neils Shi avatar image
Neils Shi answered

You can refer to the following code to solve the issue:

handlers.AddValue = function(args,context){
    var targetID = null;
    if (args && args.targetID)
        targetID = args.targetID;
        
    var friendID = null;
    if (args && args.friendID)
        friendID = args.friendID;
        
    var contentxxx = null;
    if (args && args.contentxxx)
        contentxxx = args.contentxxx;
    
    var getDataResult = server.GetUserData({
        PlayFabId : targetID,
        Keys : "gifts"
    });
    
    var jsobj = JSON.parse(getDataResult.Data["gifts"].Value);
    jsobj[friendID] = contentxxx;
    log.debug(jsobj);
    
    server.UpdateUserData({
        PlayFabId:targetID,
        Data:{"gifts":JSON.stringify(jsobj)}
    })
}

It should be noted that there are three parameters you need to pass in. The targetID(who you want to update his data), friendID(in this case, I pass "friendID3") and contentxxx(the content you want to pass in). I hope this can help you.

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.