question

vasan_routh avatar image
vasan_routh asked

Unable to get Player Data (Title) from cloud script

I attach the cloud script which I am using to get Player Data(Title), and also the PlayStream output. Its returning []. But I have player data with key named PrivateKey with proper value in PlayerData. Dont know what I am doing wrong!

//PLAYSTREAM OUTPUT
"EventName": "player_executed_cloudscript",
    "Source": "CloudScript",
    "FunctionName": "getdataa",
    "CloudScriptExecutionResult": {
        "FunctionName": "getdataa",
        "Revision": 51,
        "FunctionResult": "[]",
        "FunctionResultTooLarge": null,
        "Logs": [
            {
                "Level": "Error",
                "Message": "Event notfound. Exiting...",
                "Data": null
            }
        ],
        "LogsTooLarge": null,
        "ExecutionTimeSeconds": 0.0260343,
        "ProcessorTimeSeconds": 0.002982,
        "MemoryConsumedBytes": 4360,
        "APIRequestsIssued": 1,
        "HttpRequestsIssued": 0,
        "Error": null
    },
//CLOUD SCRIPT
handlers.getdataa = function (context) {
    var TitleDataRequest = {"Keys":["PrivateKey"]};
    var TitleDataResponse = server.GetTitleData(TitleDataRequest);
    if(!TitleDataResponse.Data.hasOwnProperty("PrivateKey"))
    {
        log.error("Event notfound. Exiting..."); 
        return JSON.stringify([]);
    }
    else
    {
	var PrivateKey = TitleDataResponse.Data.PrivateKey;
	return PrivateKey;
    }
};
Player DataapisCloudScript
10 |1200

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

Gosen Gao avatar image
Gosen Gao answered

According to your description, I think what you need is API GetUserData(not GetTitleData). This API will return the Player Data (Title). But it is important to note that data stored in Player Data can be modified easily by client. So if the data is important, I suggest you to store it in the Read Only Data(GetUserReadOnlyData) or Internal Data(GetUserInternalData).

For more information please refer to https://docs.microsoft.com/en-us/gaming/playfab/features/data/playerdata/quickstart.

10 |1200

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

vasan_routh avatar image
vasan_routh answered

@Gosen Gao While using GetUserData in cloud script there is an error popping up while parsing the JSON data. I have used the following code snippet mentioned in one of a discussion to get user data.

This is the error i got in playstream: "SyntaxError: Unexpected token o in JSON at position 1\n at JSON.parse (<anonymous>)\n at handlers.mintingRequest (F75F7-main.js:381:23)\n at Object.invokeFunction (Script:117:33)"

var request = server.GetUserData({PlayFabId: currentPlayerId, Keys: ["EnjinID", "token"]});
    var EnjID = JSON.parse(request.Data.EnjinID.Value);
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.

Gosen Gao avatar image Gosen Gao commented ·

JSON.parse needs a parameter with type of JSON String while "request.Data.EnjinID.Value" is the type of Object. That's why it said "Unexpected token o". Since it is already an Object, I think you don't need to call "JSON.parse".

2 Likes 2 ·

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.