Process Receipt Issue

I have been making a script that gives players a reward when they buy a developer product, however I keep getting this error: ServerScriptService.PlayerManager:54: attempt to index nil with ‘PlayerId’ and I don’t know why it is happening. I have made this exact script before and it worked perfectly.
This is some of the script:

local function processReceipt(receipt)
	print(receipt.PlayerId, receipt.ProductId)
	--receipt has: PlayerId, PurchaseId, ProductId, CurrencySpentValue, CurrencyType, PlaceIdWherePurchased
	local ID = receipt.PlayerId .. "-" .. receipt.PurchaseId
	
	local success = nil
	pcall(function()
		success = previousPurchases:GetAsync(ID)
	end)
	
	if success then
		--they have bought it
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

However it errors on the print statement because there is no PlayerId. Thanks in advance for any help!

1 Like

Can you show where your setting the ProcessReceipt callback for MarketPlace Service, also, ensure that your only setting it once.

This is the whole script:

local function processReceipt(receipt)
	print(receipt.PlayerId, receipt.ProductId)
	--receipt has: PlayerId, PurchaseId, ProductId, CurrencySpentValue, CurrencyType, PlaceIdWherePurchased
	local ID = receipt.PlayerId .. "-" .. receipt.PurchaseId
	
	local success = nil
	pcall(function()
		success = previousPurchases:GetAsync(ID)
	end)
	
	if success then
		--they have bought it
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	
	local player = game.Players:GetPlayerByUserId(receipt.PlayerId)
	if not player then
		--they left or disconnected
		return Enum.ProductPurchaseDecision.NotProcessedYet
		
	else
		print("Found Player!")
		if receipt.ProductId == donationId then
			--give them a cheese
			local cheese = gear.Cheese:Clone()
			cheese.Parent = player:WaitForChild("Backpack")
			cheese.Handle.Cheese.PlaybackSpeed = math.random(0.7,1.4)
		end
		
		pcall(function()
			previousPurchases:SetAsync(ID, true)
		end)
		
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	
end

marketPlaceService.ProcessReceipt = processReceipt()

Your setting the callback incorrectly in this line:

marketPlaceService.ProcessReceipt = processReceipt()
--should be:
marketPlaceService.ProcessReceipt = processReceipt
1 Like

Wow I didn’t even see that thank you!

1 Like