How to prevent ProcessReceipt from firing multiple times after purchase?

This script works when purchasing, but when you purchase the product again, it runs the function an extra time each time. Is there any way to prevent this without making the code too messy?

local MarketPlaceService = game:GetService("MarketplaceService")
local getData = game.ReplicatedStorage:FindFirstChild("editData")
local CurrencyEvent = game.ReplicatedStorage.Events.BuyCurrencyEvent


MarketPlaceService.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == 0000000 then
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		if player then
			getData:Invoke(player,function(profile)
				if profile then
					profile.Data.Coin += 100
					return Enum.ProductPurchaseDecision.PurchaseGranted
				end
			end)
		else
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
	end
end

Add a debounce.
They’re there to stop stuff from firing multiples of times. It can be set up like this:

local MarketPlaceService = game:GetService("MarketplaceService")
local getData = game.ReplicatedStorage:FindFirstChild("editData")
local CurrencyEvent = game.ReplicatedStorage.Events.BuyCurrencyEvent

local debounce = false

MarketPlaceService.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == 0000000 and debounce == false then
        
        debounce = true
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		if player then
			getData:Invoke(player,function(profile)
				if profile then
					profile.Data.Coin += 100
					return Enum.ProductPurchaseDecision.PurchaseGranted
				end
			end)
		else
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
	end
end

wait(5)
debounce = false
2 Likes

That sounds like an issue with events. Make sure you’re disconnecting any unused events because you may be creating the same events again.

I asked a friend for help, and all that they had told me was to move the return function down a couple lines, and it seems to work fine now.

local MarketPlaceService = game:GetService("MarketplaceService")
local getData = game.ReplicatedStorage:FindFirstChild("editData")
local CurrencyEvent = game.ReplicatedStorage.Events.BuyCurrencyEvent


MarketPlaceService.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == 0000000 then
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		if player then
			getData:Invoke(player,function(profile)
				if profile then
					profile.Data.Coin += 100
				end
			end)
			return Enum.ProductPurchaseDecision.PurchaseGranted
		end
	end
end

[/quote]

4 Likes

Why are you using remote functions? That makes no sense at all.

Nice thanks. Looked for a solution for like 30 minutes