ProcessReceipt not working when it's in a function

this is my code

   MS.PromptProductPurchaseFinished:Connect(function(userId,SongDevId,isPurchased)
    	local plr = game.Players:GetPlayerByUserId(userId)
    	print(plr)
    	if isPurchased == true then
    		print("PlayerPurchased Product")
    		MS.ProcessReceipt = processReceipt
    	else
    		plr.PlayerGui.SongPlayer.SongButton.ButtonOpener.Disabled = false
    	end
    end)

if I take the MS.ProcessReceipt = processReceipt ot of the function it works. But like this it doesn’t is there any way to fix this?

Not sure how it works when you “take it out of the function” since you never even defined a callback “processReceipt.” This isn’t how you’re supposed to handle a product purchase-- PromptProductPurchaseFinished is deprecated and might no longer work, I’m not sure since I haven’t stayed updated on this event. To make this work, take a look at this: MarketplaceService | Documentation - Roblox Creator Hub

1 Like

The way to fix it is to define it outside that event. Why would you only define it inside the event?

The ProcessReceipt function is only called upon Roblox requesting you to process a purchase, so nothing bad will happen if you define it like the developer hub suggests.

The reason it doesn’t work here is that the function is probably called before you’ve defined it, as the event you’ve hooked to is not a very reliable way of handling completed purchases, and is advised against on the developer hub.

1 Like

You shouldn’t use PromptProductPurchaseFinished for detecting if the purchase has gone though, you should using it for detecting if the prompt has disappeared, as least that was its original intent.

The processreceipt callback is what you want, as stated by Chatowillwin. I’m not a huge fan of the wiki’s code snippets so maybe you’ll find my code snippet useful

The second and third NotProcessedYets should never ever happen in a live server - its up to you to stop that.

1 Like