question

joeypc6 avatar image
joeypc6 asked

Grant multiple items

i'm trying to give the player an amount of item but for loop breaks the game so i need some workaround

handlers.GameMasterGift = function(args,Stack,ItemId){
    
    var Itemidss = null;
    
    if (args.Stack == 1) {
        Itemidss = args.ItemId;
    }
    else
     {
          Itemidss = args.ItemId +" ";
          
          for (var i = 1; i < args.Stack; i++) {
          Itemidss = ","+ args.ItemId + " " ;
          }
        
     }
          var result = server.GrantItemsToUser({
        // In your game, this should just be a constant
        CatalogVersion : "Items",
        PlayFabId : currentPlayerId,
        ItemIds : [ Itemidss ]
    });
}


<br><br><br><br>this don't work ,but if the stack is 1 it works, if it's more then 1 it stops and erros are showen 
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

·
Citrus Yan avatar image
Citrus Yan answered

Hi, according to the docs, the “ItemIds” field must be an array. The problem is that when you have multiple items to grant, you get a string (like this “ItemId , ItemId , ItemId” ) from the for loop and then passed in this code “ ItemIds : [ Itemidss ]”. In this way, instead of creating the array of multiple items you expected, it only created an array which contains one string element. Hence, I rewrote your code and it worked, you could try the following:

handlers.GameMasterGift = function(args){
    
    var Itemidss = [];
    
    if (args.Stack == 1) {
        Itemidss.push(args.ItemId);
    }
    else
     {
          
         for (var i = 0; i < args.Stack; i++) {
            Itemidss.push(args.ItemId)
          }
        
     }
  
          var result = server.GrantItemsToUser({
        // In your game, this should just be a constant
        CatalogVersion : "Items",
        PlayFabId : currentPlayerId,
        ItemIds : Itemidss
    });
}

This way may meet your needs, any issues let us know:)

2 comments
10 |1200

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

joeypc6 avatar image joeypc6 commented ·

Thanks it worked

0 Likes 0 ·
Citrus Yan avatar image Citrus Yan joeypc6 commented ·

Glad it helped, any issues let us know:)

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.