question

Kim Strasser avatar image
Kim Strasser asked

CloudScript: SyntaxError: Unexpected token void at Script Document

I get an error message in my cloudScript but I don't know what I'm doing wrong.

SyntaxError: Unexpected token void at Script Document [3]:7:12 -> public void ChooseanAvatar(String avatarnumber)

What is wrong with my cloudScript?

I only want to allow my avatars in my game: https://community.playfab.com/questions/33775/i-only-want-to-allow-my-avatars-in-my-game.html?childToView=33803#comment-33803

My CloudScript:

handlers.UpdatePlayerAvatar = function (args, context)
{
   var AvatarNumber = args.DesiredAvatar;
   var CurrentAvatarUrl = "";
   var NewAvatarUrl = "";
   
    public void ChooseanAvatar(String avatarnumber)
    {
        switch (avatarnumber)
        {
            case "0":
                NewAvatarUrl = "https://banner2.kisspng.com/20171216/b7c/bart-simpson-png-5a357766c9ce48.7360387215134534148266.jpg";
            break;
            case "1":
                NewAvatarUrl = "https://cdn.imgbin.com/12/23/1/imgbin-bart-simpson-homer-simpson-lisa-simpson-skateboard-bart-simpson-illustration-ejAZuguU5kURKYmP6L9Yp47s7.jpg";
            break;
            case "2":
                NewAvatarUrl = "https://www.stickpng.com/assets/thumbs/5a0c40785a997e1c2cea1168.png";
            break;
        }
    }
   
    var resultprofile = server.GetPlayerProfile(
       {
           PlayFabID: currentPlayerId, 
           ProfileConstraints : {
               ShowAvatarUrl: true
           }
       });
       
       if (resultprofile.Error == null)
       {
          if (resultprofile.PlayerProfile != null)
          {
            CurrentAvatarUrl = resultprofile.PlayerProfile.AvatarUrl;
            ChooseanAvatar(AvatarNumber);
           
            if ((NewAvatarUrl != "") && (NewAvatarUrl != CurrentAvatarUrl))
              server.UpdateAvatarUrl({PlayFabId: currentPlayerId, ImageUrl: NewAvatar});
            else
              log.info("Not possible to change Avatar:" + CurrentAvatarUrl.toString());
          }
       }
}

My client code:

 private async Task UpdateAvatarUrl()
 {
     var result = await PlayFabClientAPI.ExecuteCloudScriptAsync(new ExecuteCloudScriptRequest()
     {
         FunctionName = "UpdatePlayerAvatar",
         FunctionParameter = new { DesiredAvatar = "1" },
         GeneratePlayStreamEvent = true
     });

     if (result.Error != null)
         Console.WriteLine(result.Error.Error.ToString());
     else
     {
         if (result.Result.Logs.Count() > 0)
             Console.WriteLine(result.Result.Logs[0].Message);
         else
             Console.WriteLine("Your new Avatar: " + "1");
     }
}
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

·
Sarah Zhang avatar image
Sarah Zhang answered

Your declaration “public void” does not conform to Js's grammar and there are some spelling mistakes too. You can refer to the following code.

function ChooseanAvatar(avatarnumber)
{
    switch (avatarnumber) {
        case "0":
            return("https://banner2.kisspng.com/20171216/b7c/bart-simpson-png-5a357766c9ce48.7360387215134534148266.jpg");
            break;
        case "1":
            return("https://cdn.imgbin.com/12/23/1/imgbin-bart-simpson-homer-simpson-lisa-simpson-skateboard-bart-simpson-illustration-ejAZuguU5kURKYmP6L9Yp47s7.jpg");
            break;
        case "2":
            return("https://www.stickpng.com/assets/thumbs/5a0c40785a997e1c2cea1168.png");
            break;
    }
}

handlers.UpdatePlayerAvatar = function (args, context) {
    var avatarnumber = args.DesiredAvatar;
    var CurrentAvatarUrl = "";
    
    var NewAvatarUrl = ChooseanAvatar(args.DesiredAvatar);

    var resultprofile = server.GetPlayerProfile(
        {
            PlayFabID: currentPlayerId,
            ProfileConstraints: {
                ShowAvatarUrl: true
            }
        });

    if (resultprofile != null) {
        if (resultprofile.PlayerProfile.AvatarUrl == null) {
            server.UpdateAvatarUrl({ PlayFabId: currentPlayerId, ImageUrl: NewAvatarUrl });
            log.info("Success Upadate the first Avatar: " + NewAvatarUrl);
        }
        else {
            CurrentAvatarUrl = resultprofile.PlayerProfile.AvatarUrl;
            if ((NewAvatarUrl != "") && (NewAvatarUrl != CurrentAvatarUrl)) {
                server.UpdateAvatarUrl({ PlayFabId: currentPlayerId, ImageUrl: NewAvatarUrl });
                log.info("Success change the Avatar: " + NewAvatarUrl);
            }
            else
                log.info("Not possible to change Avatar: " + CurrentAvatarUrl);
        }
    }
}

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 ·

Thank you. It works :)

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.