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

How do I fix this code for developer product purchase?

Asked by 3 years ago

I have tried making a Donation dev product. This is the code that I want to run when at least one of the products have been bought. Any solution? Thanks.

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

local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")

local productFunctions = {}
productFunctions[1082181820] = function(receipt, player)
    game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "10")
        return true
    end
productFunctions[1082181849] = function(receipt, player)
        game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "50")
        return true
end
productFunctions[1082181878] = function(receipt, player)
        game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "100")
        return true
    end
 productFunctions[1082181916] = function(receipt, player)
        game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "150")
        return true
end
 productFunctions[1082181958] = function(receipt, player)
        game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "200")
        return true
end
 productFunctions[1082181998] = function(receipt, player)
        game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "500")
        return true
end
productFunctions[1082182029] = function(receipt, player)
        game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "1000")
        return true
end
local function processReceipt(receiptInfo)

    local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
    local purchased = false
    local success, errorMessage = pcall(function()
        purchased = purchaseHistoryStore:GetAsync(playerProductKey)
    end)
    if success and purchased then
        return Enum.ProductPurchaseDecision.PurchaseGranted
    elseif not success then
        error("Data store error:" .. errorMessage)
    end
    local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
    if not player then
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end

    local handler = productFunctions[receiptInfo.ProductId]

    local success, result = pcall(handler, receiptInfo, player)
    if not success or not result then
        warn("Error occurred while processing a product purchase")
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end

    local success, errorMessage = pcall(function()
        purchaseHistoryStore:SetAsync(playerProductKey, true)
    end)
    if not success then
        error("Cannot save purchase data: " .. errorMessage)
    end

    return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketplaceService.ProcessReceipt = processReceipt
0
Is this your whole code? User#30567 0 — 3y
0
Yes, what it does when the product has been purchased. WoofWoofWatermelonYT 16 — 3y
0
Does it even make people purchase the product User#30567 0 — 3y

Answer this question