Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my dev product system give strange amounts of currency?

Asked by 5 years ago

I made a developer product system. For some reason, if the player has more than 0 Treasure, it all the products give more Treasure than they are supposed to. Here's the code.

local marketplaceService = game:GetService("MarketplaceService")
local productID = 305518579 --100 Treasure
local productID2 = 305551012 --500 Treasure
local productID3 = 305580165 --1000 Treasure
local productID4 = 305663788 --2500 Treasure
local productID5 = 305667113 --5000 Treasure

marketplaceService.ProcessReceipt = function(receiptInfo)
    for i, player in ipairs(game.Players:GetChildren()) do
        if player.userId == receiptInfo.PlayerId then

            if receiptInfo.ProductId == productID then
                local treasure = player.leaderstats.Treasure
                local totalTreasure = player.TotalTreasure
                treasure.Value = treasure.Value + 100
                totalTreasure.Value = player.TotalTreasure.Value + 100
            end

            if receiptInfo.ProductId == productID2 then
                local treasure = player.leaderstats.Treasure
                local totalTreasure = player.TotalTreasure
                treasure.Value = treasure.Value + 500
                totalTreasure.Value = totalTreasure.Value + 500
            end

            if receiptInfo.ProductId == productID3 then
                local treasure = player.leaderstats.Treasure
                local totalTreasure = player.TotalTreasure
                treasure.Value = treasure.Value + 1000
                totalTreasure.Value = totalTreasure.Value + 1000
            end

            if receiptInfo.ProductId == productID4 then
                local treasure = player.leaderstats.Treasure
                local totalTreasure = player.TotalTreasure
                treasure.Value = treasure.Value + 2500
                totalTreasure.Value = totalTreasure.Value + 2500
            end

            if receiptInfo.ProductId == productID5 then
                local treasure = player.leaderstats.Treasure
                local totalTreasure = player.TotalTreasure
                treasure.Value = treasure.Value + 5000
                totalTreasure.Value = totalTreasure.Value + 5000
            end

        end
    end
end

2 answers

Log in to vote
0
Answered by
Elixcore 1337 Moderation Voter
5 years ago
Edited 5 years ago

the answer is: you used a for loop, therefore executing everything inside the loop multiple times.

    for i, player in ipairs(game.Players:GetChildren()) do
0
So what do i use instead? Ultimate_Miner64 45 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

Found out what I did wrong. I forgot to add the

return Enum.ProductPurchaseDecision.PurchaseGranted

at the end. I remembered that until "PurchaseGranted" is returned, ProcessReceipt() is ran every few seconds. For anyone with this problem, this may be the solution.

Answer this question