question

Leons Creative Studio avatar image
Leons Creative Studio asked

Cloudscript - Get PlayerData

Hey Everyone,

I am new to cloudscripting and i am in need of some assistance.

All of my Users get an playerdata with: "Key : Role" and it's value either (adventure, host, admin).

My idea is to have an cloudscript function "ChangeRole" that takes in 2 arguments, 1 playfabId and 1 newRole.

I want the script to check if the caller of this function has an role of admin, if it has, keep going and set the target playfabId with the new role... But i can't get an grip of why the cloudscript ain't returning the keyvalue...

handlers.ChangeRole = function (args, context) {
    
    // Cloud Script handles authenticating the player automatically.
    var UserRole;
    // here we are fetching the "SaveState" key from PlayFab,
    var playerData = playerData({"Role" : UserRole});
    //var dataValue = GetUserReadOnlyDataResponse.Data["Role"].Value;
    
    if (playerData == "Admin") {
        log.info("Current player is an admin");
         
        //Recieve target
        var inputTarget = null;
        if (args && args.hasOwnProperty("inputTarget"))
            inputTarget = args.inputTarget;
        
        //recieve targets new role
        var inputRole = null;
        if (args && args.hasOwnProperty("inputRole"))
            inputRole = args.inputRole;
            
        //var message = "Current user is an admin, change target: ";
        //log.debug("helloWorld:", { input: inputValue });


        var result = server.UpdateUserReadOnlyData({"PlayFabId" : inputTarget, "Role" : inputRole, "Permission":"Public" });


    }
    else
    {
        //var message = "Current user role is: ";
        log.info("Current player i not an admin!");
    }

    return { messageValue: message };
};

I tried this to many time and now i ain't even recieving back an message to the unity editer...

I think the error is in the playerdata, as i don't fetch the value of the key "Role"....

Hope anyone can assist me in getting this to work.

Best regards an new hopeful PlayFab user

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

·
Seth Du avatar image
Seth Du answered

Please understand executing the PlayFab API, a valid request is required, in the meantime, the response will be well-structured. If you don’t know the details of the response. I suggest you to use Postman(RESTful testing tools) to help you quickly troubleshoot or learn the usage.

Here is a sample of getting the Role in Cloud Script function, assuming you store the Role in Game Manager like the following:

var checkRequest = {
    "PlayFabId": "xxxxxxxxxx",
    "Keys": [
        "Role"
        ]
    
};
var checkResult = server.GetUserReadOnlyData(checkRequest);
var UserRole = checkResult.Data.Role.Value;

In addition, I notice that the request of UpdateUserReadOnlyData is not valid. {“Role”: inputRole } should be the value for the property “Data”. The key(Property name) is predefined. Please refer to the API documentation for the use:


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.