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

Having trouble with handling more than one product purchase?

Asked by 5 years ago

I am having trouble creating a script that reward points after buying the developer product. The points are for a system I am using in-game that involves getting points via leaderstats. Whenever I try to add more than one ProccessReceipt, only the last one I added works.

Heres the code I am trying:

local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")

local currencyName = "A.P"

local PreviousPurchases = DataStoreService:GetDataStore("PreviousPurchases")

local bit_ap = 443047981
local some_ap = 443047297
local pocket_ap = 443047667
local hand_ap = 443046726
local lot_ap = 443048522
local heart_ap = 443049632
local insane_ap = 443051526

MarketplaceService.ProcessReceipt = function(receipt) --150 Ap
    -- Receipt has PurchaseID, PlayerID, ProdutID, CurrecnySpentValue, CurrencyType, PlaceIdWherePurchased

    local ID = receipt.PlayerId.."-"..receipt.PurchaseId

    local success = nil

    pcall(function()
        success = PreviousPurchases:GetAsync(ID)
    end)

    if PreviousPurchases:GetAsync(ID) then -- Has it been bought?
        -- Purchase has been done
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end

    local player = game.Players:GetPlayerByUserId(receipt.PlayerId)

    if not player then
        -- Left, disconnected
        return Enum.ProductPurchaseDecision.NotProcessedYet -- Give rewards on next join
    else

        if receipt.ProductId == bit_ap then
            -- They bought 125 ap
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 150
        end

        pcall(function()
            PreviousPurchases:SetAsync(ID,true)
        end)
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end
end
MarketplaceService.ProcessReceipt = function(receipt) --500 Ap

    -- Receipt has PurchaseID, PlayerID, ProdutID, CurrecnySpentValue, CurrencyType, PlaceIdWherePurchased

    local ID = receipt.PlayerId.."-"..receipt.PurchaseId

    local success = nil

    pcall(function()
        success = PreviousPurchases:GetAsync(ID)
    end)

    if PreviousPurchases:GetAsync(ID) then -- Has it been bought?
        -- Purchase has been done
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end

    local player = game.Players:GetPlayerByUserId(receipt.PlayerId)

    if not player then
        -- Left, disconnected
        return Enum.ProductPurchaseDecision.NotProcessedYet -- Give rewards on next join
    else

        if receipt.ProductId == some_ap then
            -- They bought 125 ap
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 500
        end

        pcall(function()
            PreviousPurchases:SetAsync(ID,true)
        end)
        return Enum.ProductPurchaseDecision.PurchaseGranted


    end
end     
MarketplaceService.ProcessReceipt = function(receipt) --750 Ap
    -- Receipt has PurchaseID, PlayerID, ProdutID, CurrecnySpentValue, CurrencyType, PlaceIdWherePurchased

    local ID = receipt.PlayerId.."-"..receipt.PurchaseId

    local success = nil

    pcall(function()
        success = PreviousPurchases:GetAsync(ID)
    end)

    if PreviousPurchases:GetAsync(ID) then -- Has it been bought?
        -- Purchase has been done
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end

    local player = game.Players:GetPlayerByUserId(receipt.PlayerId)

    if not player then
        -- Left, disconnected
        return Enum.ProductPurchaseDecision.NotProcessedYet -- Give rewards on next join
    else

        if receipt.ProductId == pocket_ap then
            -- They bought 125 ap
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 750
        end

        pcall(function()
            PreviousPurchases:SetAsync(ID,true)
        end)
        return Enum.ProductPurchaseDecision.PurchaseGranted


    end
end
MarketplaceService.ProcessReceipt = function(receipt) --1000 Ap
    -- Receipt has PurchaseID, PlayerID, ProdutID, CurrecnySpentValue, CurrencyType, PlaceIdWherePurchased

    local ID = receipt.PlayerId.."-"..receipt.PurchaseId

    local success = nil

    pcall(function()
        success = PreviousPurchases:GetAsync(ID)
    end)

    if PreviousPurchases:GetAsync(ID) then -- Has it been bought?
        -- Purchase has been done
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end

    local player = game.Players:GetPlayerByUserId(receipt.PlayerId)

    if not player then
        -- Left, disconnected
        return Enum.ProductPurchaseDecision.NotProcessedYet -- Give rewards on next join
    else

        if receipt.ProductId == hand_ap then
            -- They bought 125 ap
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 1000
        end

        pcall(function()
            PreviousPurchases:SetAsync(ID,true)
        end)
        return Enum.ProductPurchaseDecision.PurchaseGranted


    end
end
MarketplaceService.ProcessReceipt = function(receipt) --2000 Ap
    -- Receipt has PurchaseID, PlayerID, ProdutID, CurrecnySpentValue, CurrencyType, PlaceIdWherePurchased

    local ID = receipt.PlayerId.."-"..receipt.PurchaseId

    local success = nil

    pcall(function()
        success = PreviousPurchases:GetAsync(ID)
    end)

    if PreviousPurchases:GetAsync(ID) then -- Has it been bought?
        -- Purchase has been done
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end

    local player = game.Players:GetPlayerByUserId(receipt.PlayerId)

    if not player then
        -- Left, disconnected
        return Enum.ProductPurchaseDecision.NotProcessedYet -- Give rewards on next join
    else

        if receipt.ProductId == lot_ap then
            -- They bought 125 ap
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 2000
        end

        pcall(function()
            PreviousPurchases:SetAsync(ID,true)
        end)
        return Enum.ProductPurchaseDecision.PurchaseGranted


    end
end
MarketplaceService.ProcessReceipt = function(receipt) --7500 Ap
    -- Receipt has PurchaseID, PlayerID, ProdutID, CurrecnySpentValue, CurrencyType, PlaceIdWherePurchased

    local ID = receipt.PlayerId.."-"..receipt.PurchaseId

    local success = nil

    pcall(function()
        success = PreviousPurchases:GetAsync(ID)
    end)

    if PreviousPurchases:GetAsync(ID) then -- Has it been bought?
        -- Purchase has been done
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end

    local player = game.Players:GetPlayerByUserId(receipt.PlayerId)

    if not player then
        -- Left, disconnected
        return Enum.ProductPurchaseDecision.NotProcessedYet -- Give rewards on next join
    else

        if receipt.ProductId == heart_ap then
            -- They bought 125 ap
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 7500
        end

        pcall(function()
            PreviousPurchases:SetAsync(ID,true)
        end)
        return Enum.ProductPurchaseDecision.PurchaseGranted


    end
end
MarketplaceService.ProcessReceipt = function(receipt) --10000 Ap
    -- Receipt has PurchaseID, PlayerID, ProdutID, CurrecnySpentValue, CurrencyType, PlaceIdWherePurchased

    local ID = receipt.PlayerId.."-"..receipt.PurchaseId

    local success = nil

    pcall(function()
        success = PreviousPurchases:GetAsync(ID)
    end)

    if PreviousPurchases:GetAsync(ID) then -- Has it been bought?
        -- Purchase has been done
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end

    local player = game.Players:GetPlayerByUserId(receipt.PlayerId)

    if not player then
        -- Left, disconnected
        return Enum.ProductPurchaseDecision.NotProcessedYet -- Give rewards on next join
    else

        if receipt.ProductId == insane_ap then
            -- They bought 125 ap
            player.leaderstats[currencyName].Value = player.leaderstats[currencyName].Value + 10000
        end

        pcall(function()
            PreviousPurchases:SetAsync(ID,true)
        end)
        return Enum.ProductPurchaseDecision.PurchaseGranted


    end
end

And I know its not the gui's either. Please help, thanks!

0
Hecc, I ain't reading 266 lines Rare_tendo 3000 — 5y
0
Thanks! TheDeepH4x0r 20 — 5y
0
ProcessReceipt should be set once. Others will just override what you have. Tables are key to beaing able to process multiple dev products. User#5423 17 — 5y

Answer this question