question

Ozan Yilmaz avatar image
Ozan Yilmaz asked

Stackable item doesn't appear in inventory? [Possible Bug]

Hello everyone,

One of our users said that he isn't able to buy a stackable item by a virtual currency in our game. When I looked at the inventory, I saw this:

To give more information, I make the purchases by using Cloudscript. I'm not using the Purchase API.

As it can be seen from the image, the first attempt of buying "Yellow Container" looks like succeeded. However, the inventory says expired and the usage left is 0. The next one looks like succeeded too, however, it's red stacked. (The revoked one is my action. I clicked the Revoke button near it to see if it will work. It was same as the others.)

When all of the purchases were made, the related virtual currency didn't decrease in that process. So that I thought, the Cloudscript function throws an error before executing the reduction line. However, the client doesn't see any error, which means the Cloudscript returns the correct value. If it had returned a different value, the client would've seen an error message. I also tried executing the same Cloudscript function by using "Cloud Script" tab in the player's profile and the script is executed successfully, and the item was in the inventory.

The only thing I can think of is that the Cloudscript function failed while executing it. The function granted a yellow container to the player and then it failed somewhere in the code. That's why they are red stacked. However, when I tried to execute the same function over the same player, it didn't give me any error. What could be the issue?

001.jpg (126.8 KiB)
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.

Ivan Cai avatar image Ivan Cai ♦ commented ·

Can you provide your cloudscript function code?

0 Likes 0 ·

1 Answer

·
Ozan Yilmaz avatar image
Ozan Yilmaz answered

@Ivan Cai

The code didn't fit in the comment, so I'm posting here as an answer.

EDIT: This issue happened to only one user. We haven't had this issue before.

handlers.BuyContainer = function(args, context) {
    if(args == null || args.ItemID == null || args.BuyAmount == null) return 0;
    if(args.ItemID === "" || args.BuyAmount === "" || isNaN(args.BuyAmount)) return 1;

    // GetItemByID returns the related item from the catalog
    var item = GetItemByID(args.ItemID);
    if(item == null)
        return 2;

    if(item.ItemClass != "LootBox")
        return 3;

    var itemCustomData = JSON.parse(item.CustomData);
    if(parseInt(itemCustomData["Buyable"]) != 1)
        return 4;

    var amount = parseInt(args.BuyAmount);
    // GetInstanceByID returns the stackable item in the player's inventory if there is any. If not, it returns null
    var instance = GetInstanceByID(currentPlayerId, args.ItemID);
    if(instance != null) {
        var total = instance.RemainingUses != null ? parseInt(instance.RemainingUses) : 1;
        if(total + amount > 200)
            return 5;
    }
    else if(amount > 200)
        return 5;

    var getDiscount = {
        "Keys": ["DiscountContainer"]
    };
    var getDiscountResult = server.GetTitleData(getDiscount);

    var price = parseInt(item.VirtualCurrencyPrices["CO"]);
    var discount = price * (parseFloat(getDiscountResult.Data["DiscountContainer"]) / 100.0);
    var finalPrice = (Math.floor(price - discount) * amount) + 1;

    var getCoins = {
        "PlayFabId": currentPlayerId,
        "Amount": 1,
        "VirtualCurrency": "CO"
    };
    var getCoinsResult = server.AddUserVirtualCurrency(getCoins);
    var myCoins = parseInt(getCoinsResult.Balance);

    if(myCoins < finalPrice) {
        var removeCoins = {
            "PlayFabId": currentPlayerId,
            "Amount": 1,
            "VirtualCurrency": "CO"
        };
        server.SubtractUserVirtualCurrency(removeCoins);

        return { "Coins": (myCoins - 1) };
    }
    
    var decreaseCoins = {
        "PlayFabId": currentPlayerId,
        "Amount": finalPrice,
        "VirtualCurrency": "CO"
    };
    server.SubtractUserVirtualCurrency(decreaseCoins);

    if(instance == null) {
        var newContainer = {
            "PlayFabId": currentPlayerId,
            "CatalogVersion": "Items",
            "ItemIds": [item.ItemId]
        };
        var newContainerResult = server.GrantItemsToUser(newContainer);

        amount--;
        var addInstance = {
            "PlayFabId": currentPlayerId,
            "ItemInstanceId": newContainerResult.ItemGrantResults[0].ItemInstanceId,
            "UsesToAdd": amount
        }
        server.ModifyItemUses(addInstance);

        return { "InstanceID": newContainerResult.ItemGrantResults[0].ItemInstanceId };
    }
    else {
        var addInstance = {
            "PlayFabId": currentPlayerId,
            "ItemInstanceId": instance.ItemInstanceId,
            "UsesToAdd": amount
        }
        server.ModifyItemUses(addInstance);

        return { "InstanceID": instance.ItemInstanceId };
    }
};

5 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.

Ivan Cai avatar image Ivan Cai ♦ commented ·

May l have your title id and that player's playfabId to investigate?

0 Likes 0 ·
Ozan Yilmaz avatar image Ozan Yilmaz Ivan Cai ♦ commented ·

@Ivan Cai

This is my test account. If only Title ID is not enough to find it, I can send another comment from my other account. Title ID is DABE1, the player's ID is 15011DA3990C254E

0 Likes 0 ·
Ivan Cai avatar image Ivan Cai ♦ Ozan Yilmaz commented ·

Can you provide your GetItemByID and GetInstanceByID function code ? I want to restitute more accuratly.

0 Likes 0 ·
Show more comments
Show more comments

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.