ProcessReceipt firing multiple times?

Every time I purchase the product, the functions stack onto each other. After purchasing it once, it’ll call the function twice the next time I purchase it, and so on.

The script


local Market = game:GetService("MarketplaceService")

local StagesFolder = workspace:WaitForChild("Stages")
local Ids = require(game.ReplicatedStorage:WaitForChild("Ids"))




local productFunctions = {}

productFunctions["SkipStage"] = function(receipt, player)
	local ls = player:FindFirstChild("leaderstats")
	if ls then
		local stage = ls:FindFirstChild("Stage")
		if stage then
			stage.Value = stage.Value + 1
			player:LoadCharacter()
			return true
		end
	end
end

local function processReceipt(receiptInfo)
	if not receiptInfo.PlayerId then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then 
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	local name = nil
	for i,v in pairs(Ids.Products) do
		if v and i then
			if v == receiptInfo.ProductId then
				name = i
			end
		end
	end
	local handler = productFunctions[name]
	local success, result = pcall(function()
		handler(receiptInfo, player)
	end)
	if not success or not result then
		warn("Error occurred while processing a product purchase")
		print("\nProductId:", receiptInfo.ProductId)
		print("\nPlayer:", player)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end



	-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

Market.ProcessReceipt = processReceipt
1 Like

Are you sure they are completing successfully each time?

I’m using studio to test these out, and this is what it prints.

Are you sure that you’re passing through a correct devproduct id?
Also, do you have API services allowed in studio for the game?

Nvm I fixed, found out that I needed to return the function in the pcall. Thanks for your help though.

local success, result = pcall(function()
	return handler(receiptInfo, player)
end)
2 Likes