DevHub example of ProcessReceipt doesn't work

Using the DevHub example of ProcessReceipt I print and do other stuff when a product is bought. However, “HI GUYS” doesn’t print:

productFunctions[PRODUCT_ID] = function(receipt, player)
	print("HI GUYS")
	local playerInfo = player:FindFirstChild("PlayerInfo")
	local coins = playerInfo and playerInfo.Values.Coins
	if coins then
		coins.Value += 50
		return true
	end
end

-- The core 'ProcessReceipt' callback function
local function processReceipt(receiptInfo)
	-- Determine if the product was already granted by checking the data store  
	local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId

	local success, isPurchaseRecorded = pcall(function()
		return purchaseHistoryStore:UpdateAsync(playerProductKey, function(alreadyPurchased)
			if alreadyPurchased then
				return true
			end

			-- Find the player who made the purchase in the server
			local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
			if not player then
				-- The player probably left the game
				-- If they come back, the callback will be called again
				return nil
			end

			local handler = productFunctions[receiptInfo.ProductId]

			local success, result = pcall(handler, receiptInfo, player)
			-- If granting the product failed, do NOT record the purchase in datastores.
			if not success or not result then
				error("Failed to process a product purchase for ProductId:", receiptInfo.ProductId, " Player:", player)
				return nil
			end

			-- Record the transcation in purchaseHistoryStore.
			return true
		end)
	end)

	if not success then
		error("Failed to process receipt due to data store error.")
		return Enum.ProductPurchaseDecision.NotProcessedYet
	elseif isPurchaseRecorded == nil then
		-- Didn't update the value in data store.
		return Enum.ProductPurchaseDecision.NotProcessedYet
	else	
		-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

-- Set the callback; this can only be done once by one script on the server! 
MarketPlaceService.ProcessReceipt = processReceipt

PRODUCT_ID is meant to the be the id of the developer product that should have that specific function

PRODUCT_ID is already set, just not included that. I am sure that the ID is for a product owned by the game because I safely checked that… 3 times

Can you add some more prints around the process receipt to see where the code stops working? Looking through doesn’t easily show anything wrong

Using the following function, only 1, 2 and 7 print:

local function processReceipt(receiptInfo)
	-- Determine if the product was already granted by checking the data store  
	local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
	print("1")
	local success, isPurchaseRecorded = pcall(function()
		print("2")
		return purchaseHistoryStore:UpdateAsync(playerProductKey, function(alreadyPurchased)
			print("3")
			if alreadyPurchased then
				return true
			end

			-- Find the player who made the purchase in the server
			print("4")
			local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
			if not player then
				-- The player probably left the game
				-- If they come back, the callback will be called again
				return nil
			end
			print("5")
			local handler = productFunctions[receiptInfo.ProductId]

			local success, result = pcall(handler, receiptInfo, player)
			-- If granting the product failed, do NOT record the purchase in datastores.
			if not success or not result then
				error("Failed to process a product purchase for ProductId:", receiptInfo.ProductId, " Player:", player)
				return nil
			end
			print("6")
			-- Record the transcation in purchaseHistoryStore.
			return true
		end)
	end)
	print("7")
	if not success then
		error("Failed to process receipt due to data store error.")
		return Enum.ProductPurchaseDecision.NotProcessedYet
	elseif isPurchaseRecorded == nil then
		-- Didn't update the value in data store.
		return Enum.ProductPurchaseDecision.NotProcessedYet
	else	
		-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

I think I might have to make my own ProcessReceipt function

I think you can make do without the datastore stuff, checking out their new wiki shows that they don’t do anything with datastoring, try using their new example instead?

1 Like

Alright, I’ll edit the script and let you know how that goes

Well that seemed to work, making the script in ServerScriptService and not the Shop Menu made it globally too, thanks!

1 Like

Did you attempt to use the snippet inside a local script? ‘DataStore’ instances can only be accessed by the server.

It was already on a server script, both times.