MarketplaceService.ProcessReceipt calling by itself?

I just started trying out Developer products today… and it’s kinda throwing me off, along with the lack of details in documentation on for it on the wiki.

So far I am trying to make a counter of how many times a product has been purchased.

What I am getting is the counter adding up even when I don’t click the button to purchase anything.

My code so far:

local MarketplaceService = game:GetService(“MarketplaceService”)
local testID = 19352390
local PurchaseHistory = game:GetService(“DataStoreService”):GetDataStore(“PurchaseHistory”)
local DataStore = game:GetService(“DataStoreService”):GetDataStore(“TimesBought”)
local PStore = game:GetService(“DataStoreService”):GetDataStore(“PurchaseID”)

MarketplaceService.ProcessReceipt = function(receiptInfo)
for i, player in ipairs(game.Players:GetChildren()) do
if player.userId == receiptInfo.PlayerId then
local key = “user_” … player.userId
local previousId = PStore:GetAsync(key) or “”
if receiptInfo.ProductId == testID then

			if previousId == "" or recieptInfo.PurchaseId ~= previousId then

				player.PlayerGui.ScreenGui.Status.Text = "Test1 Bought"	
				DataStore:UpdateAsync(key, function(oldValue)
					local newValue = oldValue or 0 --oldValue might be nil
					newValue = newValue + 1
					player.PlayerGui.ScreenGui.Times.Text = "Times Bought : " ..newValue
					return newValue
				end)

				PStore:UpdateAsync(key, function(oldValue)
					local newValue = oldValue or "" --oldValue might be nil
					newValue = recieptInfo.PurchaseId
					return newValue
				end)
				player.PlayerGui.ScreenGui.ID.Text = receiptInfo.PurchaseId

			end

        end
    end	
end

-- record the transaction in a Data Store
local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.PurchaseId
PurchaseHistory:IncrementAsync(playerProductKey, 1)	
return Enum.ProductPurchaseDecision.PurchaseGranted		

end


Sorry the code here doesn’t indent, otherwise it would be easier to understand

Wiki tells me that this function is invoked repeatedly AFTER a player has made a purchase until it returns the purchase decision ProductGranted value or something.

But if the function is going to run multiple times, how do you make it change the counter only once?

Such as: how do I prevent an item from being given out multiple times to a player who has only purchased the product once?

By using the unique PurchaseId

Attempt it in the code, didn’t work. What am I doing wrong?

If the processreceipt is going to invoke multiple times on the same purchase, wouldn’t the purchase ID be the same anyways?

Just to let you know, we do have BBCode, which means we have the code tags.

Plus, a pastebin URL might be helpful too. If you don’t want people finding it, you can set it to an unlisted paste.

[quote] Just to let you know, we do have BBCode, which means we have the code tags.

Plus, a pastebin URL might be helpful too. If you don’t want people finding it, you can set it to an unlisted paste. [/quote]

You know people can’t watch this forum right?
but people can watch pastebin.

[quote] Just to let you know, we do have BBCode, which means we have the code tags.

Plus, a pastebin URL might be helpful too. If you don’t want people finding it, you can set it to an unlisted paste. [/quote]

You know people can’t watch this forum right?
but people can watch pastebin.[/quote]

Yes, that’s why I suggested setting it to an unlisted paste. Only people with the link will be able to find it if it’s unlisted.