ProcessReceipt Not Working As Indented (Help Needed)

The problem I am having is that when I run this code and purchase something using :PromptProductPurchase(), a list of ALL products I have bought get printed, when I am only wanting to print the purchased product. Code is provided below:

local MarketplaceService = game:GetService("MarketplaceService")

MarketplaceService.ProcessReceipt = function(receiptInfo)	
	print(receiptInfo.ProductId) -- All products I purchased are being printed? Not what I'm wanting.

	-- Product A
	if receiptInfo.ProductId == 0 then
		local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
		if player then
		    print("Product A has been purchased!")
		end
	end

        -- Product B
	if receiptInfo.ProductId == 0 then
		local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
		if player then
		    print("Product B has been purchased!")
		end
	end

-- Both products are being called when I purchase 1 item, not what I'm wanting!
end
local MarketplaceService = game:GetService("MarketplaceService")

MarketplaceService.ProcessReceipt = function(receiptInfo)	
	print(receiptInfo.ProductId) -- All products I purchased are being printed? Not what I'm wanting.

	-- Product A
	if receiptInfo.ProductId == 0 then
		local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
		if player then
			print("Product A has been purchased!")
			return Enum.ProductPurchaseDecision.PurchaseGranted
		else
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
	end

	-- Product B
	if receiptInfo.ProductId == 0 then
		local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
		if player then
			print("Product B has been purchased!")
			return Enum.ProductPurchaseDecision.PurchaseGranted
		else
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
	end

	-- Both products are being called when I purchase 1 item, not what I'm wanting!
end

I think you forgot to return the purchase direction. You also don’t need to write that many if statements.

1 Like
warn("Process Receipt Callback Will Be Assigned In This Script Instance.")
local productFunctions = {}


script.AddProduct.Event:Connect(function(productId, func)
	local success, err = pcall(function()
		productFunctions[productId] = func
	end)
	if not success then
		warn("(ProcessReceipt) Error while adding product id: "..productId or -1)
		print("\nError: "..err)
	end
end)
script.RemoveProduct.Event:Connect(function(productId)
	local success, err = pcall(function()
		productFunctions[productId] = nil
	end)
	if not success then
		warn("(ProcessReceipt) Error while removing product id: "..productId or -1)
		print("\nError: "..err)
	end
end)



productFunctions[PRODUCT_ID] = function(receiptInfo, player)
	print(player.Name.." has just bought the product with id: "..receiptInfo.ProductId)
	return true
end


local function processReceipt(receiptInfo)
	local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	local success, result = pcall(productFunctions[receiptInfo.ProductId], receiptInfo, player)
	if not success or not result then
		warn("(ProcessReceipt) Error occurred while processing a product purchase")
		print("\nProductId:", receiptInfo.ProductId)
		print("\nPlayer:", player)
		print("\nError/Result: "..result)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted
end


game:GetService("MarketplaceService").ProcessReceipt = processReceipt

image

I added this to the code and it still gave me the same issue.

local Plr = Players:GetPlayerFromUserId(receiptInfo.PlayerId)

if Plr then
if receiptInfo.ProductId == (product A) then
print("A")
elseif receiptInfo.ProductId == (product B) then
print("B")
end
return Enum.ProductPurchaseDirection.PurchaseGranted
else
return Enum.ProductPurchaseDirection.NotProcessedYet
end

Maybe this will work better. You only need to use one if statement for the product IDs. I might have spelled some stuff wrong but you should get the point.

1 Like

I added this to the code and it worked, thank you!

1 Like