question

Kim Strasser avatar image
Kim Strasser asked

Cloud Script always fails

I use the following cloud script to add virtual currency(GO) to a user's PlayFab account. I get no error message in the client code and Console.WriteLine("Added reward!"); is executed, but Playstream always shows FAILED and the user's virtual currency doesn't get the increment.

Do you know what is wrong?

My cloud script:

handlers.AddGoldCoins = function (args, context)
{
    if (args.rewardType == "Bronze")
    {
      server.AddUserVirtualCurrency({PlayFabID: currentPlayerID, VirtualCurrency: "GO", Amount: "100"});
    }
    if (args.rewardType == "Silver")
    {
      server.AddUserVirtualCurrency({PlayFabID: currentPlayerID, VirtualCurrency: "GO", Amount: "200"});
    }
    if (args.rewardType == "Gold")
    {
      server.AddUserVirtualCurrency({PlayFabID: currentPlayerID, VirtualCurrency: "GO", Amount: "500"});
    }
    return {messageValue: "added reward"};
}

Client code:

private async Task AddVirtualCurrency()
{
  var result = await PlayFabClientAPI.ExecuteCloudScriptAsync(new ExecuteCloudScriptRequest()
  {
    FunctionName = "AddGoldCoins",
    FunctionParameter = new { rewardType = "Bronze" },
    GeneratePlayStreamEvent = true
  });

  if (result.Error != null)
  {
    Console.WriteLine(result.Error.GenerateErrorReport());
  }
  else
  {
    Console.WriteLine("Added reward!");
  }
}

In Playstream, I get this error message:

Player executed Cloud Script FAILED: AddGoldCoins() (revision 6)

Raw event JSON
{
    "EventName": "player_executed_cloudscript",
    "Source": "CloudScript",
    "FunctionName": "AddGoldCoins",
    "CloudScriptExecutionResult": {
        "FunctionName": "AddGoldCoins",
        "Revision": 6,
        "FunctionResult": null,
        "FunctionResultTooLarge": null,
        "Logs": [],
        "LogsTooLarge": null,
        "ExecutionTimeSeconds": 0.0004205,
        "ProcessorTimeSeconds": 0,
        "MemoryConsumedBytes": 28088,
        "APIRequestsIssued": 0,
        "HttpRequestsIssued": 0,
        "Error": {
            "Error": "JavascriptException",
            "Message": "JavascriptException",
            "StackTrace": "ReferenceError: currentPlayerID is not defined\n    at handlers.AddGoldCoins (BFD0A-main.js:6:49)"
        }
    },
    "EventNamespace": "com.playfab",
    "EntityType": "player",
    "TitleId": "BFD0A",
    "EntityId": "4BDFD55D07F2D3F6",
    "EventId": "b2504b4f8a924b8b94d7724b27508669",
    "SourceType": "BackEnd",
    "Timestamp": "2019-05-30T22:19:05.8230987Z",
    "History": null,
    "CustomTags": null,
    "Reserved": null,
    "PlayFabEnvironment": {
        "Vertical": "master",
        "Cloud": "main",
        "Application": "logicserver",
        "Commit": "9ebcb67"
    }
}
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

·
brandon@uprootstudios.com avatar image
brandon@uprootstudios.com answered

The error is that "currentPlayerID is not defined". Have no fear, the only thing you're doing wrong is a simple typo!

Change "currentPlayerID" to "currentPlayerId" (note the lowercase 'd') in your cloud code, and the issue should be resolved. Let me know if it works.

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.

Kim Strasser avatar image Kim Strasser commented ·

Thanx. It works now :)

1 Like 1 ·

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.