question

Kim Strasser avatar image
Kim Strasser asked

How can I use server.GrantItemsToUser in Cloud Script?

I don't know how to call server.GrantItemsToUser in Cloud script. I get this error message:

"Error": {
      "Message": "The script called a PlayFab API, which returned an error. See the Error logs for details.",
      "Error": "CloudScriptAPIRequestError",
      "StackTrace": "Error\n    at Object.server_request (Script:180:24)\n    at Object.server.GrantItemsToUser (Script:565:73)\n    at handlers.GrantLeaderboardRewardsToAllPlayers (BFD0A-main.js:74:24)\n    at Object.invokeFunction (Script:116:33)"
    },
{
        "Message": "PlayFab API request error",
        "Level": "Error",
        "Data": {
          "apiError": {
            "errorDetails": {
              "ItemIds": [
                "invalid item at index 0: The ItemIds field is required."
              ]
            },

How can I add one or more Item Ids in the ItemIds field? I want to grant 2 different items to this PlayFabID.

var rewards = "ItemTitaniumSword";
var result = server.GrantItemsToUser(
{
    PlayFabID: currentPlayerId,
    CatalogVersion : "New Shop",
    ItemIds : [{ rewards }]
});
   
if ((result != null) && (result.Error == null))
{
    if (result.ItemGrantResults[0] > 0)
    {
        if (result.ItemGrantResults[0].Result == true)
            GrantingItemSuccessful = true;
        else
            GrantingItemSuccessful = false;
    }
    else
        GrantingItemSuccessful = false;
}
else
    GrantingItemSuccessful = false;
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.

1 Answer

·
Seth Du avatar image
Seth Du answered

You may try the following in the Cloud Script:

var result = server.GrantItemsToUser(
{
 PlayFabID: currentPlayerId,
 CatalogVersion : "New Shop",
 ItemIds : [rewards]
});

The format of property ItemIds is not correct and it is a list of string instead of a list of object.

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.