question

Muhammad Roshaan Tariq avatar image
Muhammad Roshaan Tariq asked

How to access Player data using cloudscript

Hi, I am coding some functions in cloudscript and I want to access the player data along with it's specific variables out of JSON

Now I only want to access the level variable from this Playerdata JSON, Is there any way I can do this? OR do I have to pass it from client side which I don't want to do because of some security concerns of data.

Player DataCloudScript
1.jpg (57.0 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

·
Sarah Zhang avatar image
Sarah Zhang answered

You can use server API GetUserData on CloudScript to get the entire JSON string value of this PlayerDate K/V pair, then use JSON.parse([JSONstring]) function to parse the JSON string to JSON object to get the level’s value. For example, when the key of this PlayerData K/V pair is “Info”, the CloudScript function would be something like this.

handlers.getPlayerDataJSONValue = function (args, context) {
    var getPlayerInfo = server.GetUserData
        ({
            PlayFabId: currentPlayerId,
            Keys: ["Info"],
        });
    var playerInfoObject = JSON.parse(getPlayerInfo.Data.Info.Value);
    var playerLevel = playerInfoObject.level;
    return playerLevel;
}
7 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.

Muhammad Roshaan Tariq avatar image Muhammad Roshaan Tariq commented ·
@Sarah Zhang

What if I want to add some amount in playerLevel using cloudscript then how can I save the data or assign back the value to player data? How to do that?

0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang Muhammad Roshaan Tariq commented ·
function getPlayerDataJSONValue() {
    var getPlayerInfo = server.GetUserData
        ({
            PlayFabId: currentPlayerId,
            Keys: ["Info"],
        });
    var playerInfoObject = JSON.parse(getPlayerInfo.Data.Info.Value);
    var getPlayerDataJSONValue = playerInfoObject.level;
    return { playerLevel: getPlayerDataJSONValue, playerInfoObject: playerInfoObject }
}



handlers.updatePlayerlevel = function (args, context) {
    var playerInfoObject = getPlayerDataJSONValue().playerInfoObject;
    playerInfoObject.level = playerInfoObject.level + 1;
    var playInfoStr = JSON.stringify(playerInfoObject);
    var updataPlayerlevelResult = server.UpdateUserData
        ({
            PlayFabId: currentPlayerId,
            Data: { "Info": playInfoStr }
        });

    return updataPlayerlevelResult;
}

0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang Sarah Zhang commented ·

Besides, we noticed you mentioned “security concerns of data”. PlayFab Player Data is readable and writable by the client, please use Player Internal Data if you want players can only read the data but cannot write it. If so the CloudScript code would be something like this.

function getPlayerDataJSONValue() {
    var getPlayerInfo = server.GetUserInternalData
        ({
            PlayFabId: currentPlayerId,
            Keys: ["Info"],
        });
    var playerInfoObject = JSON.parse(getPlayerInfo.Data.Info.Value);
    var getPlayerDataJSONValue = playerInfoObject.level;
    return { playerLevel: getPlayerDataJSONValue, playerInfoObject: playerInfoObject }
}



handlers.updatePlayerlevel = function (args, context) {
    var playerInfoObject = getPlayerDataJSONValue().playerInfoObject;
    playerInfoObject.level = playerInfoObject.level + 1;
    var playInfoStr = JSON.stringify(playerInfoObject);
    var updataPlayerlevelResult = server.UpdateUserInternalData
        ({
            PlayFabId: currentPlayerId,
            Data: { "Info": playInfoStr }
        });

    return updataPlayerlevelResult;
}



0 Likes 0 ·
Show more comments
Muhammad Roshaan Tariq avatar image Muhammad Roshaan Tariq Muhammad Roshaan Tariq commented ·

@Sarah Zhang

thanks for code snippet. And my main concern in security was that I don't want this data to be manipulated from client side so I was thinking to use cloudscript to add or edit. But thanks for mentioning the internal data API

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.