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
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.
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