I'm currently trying to make a developer product. I'm using the wiki, and so far, I understand everything except for a single line. The line I'm confused about is as follows:
MarketplaceService.ProcessReceipt = function(receiptInfo)
The entire script is as follows:
local MarketplaceService = Game:GetService("MarketplaceService") MarketplaceService.ProcessReceipt = function(receiptInfo) game.Workspace.DisplayScreen.SurfaceGui.TextBox.Text = receiptInfo.PlayerId .. " just bought " .. receiptInfo.ProductId -- ... -- use DataStore to record purchase -- ... return Enum.ProductPurchaseDecision.PurchaseGranted end
This is the first time I've ever seen anything like this. Can someone dissect this line for me, bit by bit?
That line starts a function which is called when a product is brought.
local MarketplaceService = Game:GetService("MarketplaceService") MarketplaceService.ProcessReceipt = function(receiptInfo) -- ... stuff to run when something is brought goes here... end
MarketplaceService
is the service. ProcessReceipt
is a variable that is a function, and it is ran when a purchase is made. You're setting it to a function.
After a purchase is made it is up to the developer to handle and record the transaction. To do this, the developer has to define the ProcessReceipt callback function. After a player has purchased an item, this function will be called repetitively until the callback returns Enum.ProductPurchaseDecision.PurchaseGranted. This function will be passed the following arguments in a table:
-number PlayerId - The Id of the player making the purchase. -number PlaceIdWherePurchased - The specific place where the purchase was made. -string PurchaseId - A unique identifier for the purchase, should be used to prevent granting an item multiple times for one purchase. -number ProductId - The Id of the purchased product. -Enum.CurrencyType CurrencyType - The type of currency used. -number CurrencySpent - The amount of currency spent on the product for this purchase. -As long as the player is in the game, the ProcessReceipt function will be called every few seconds. If the player leaves, the function will stop being called, but will resume if the player rejoins the game. As soon as this function returns PurchaseGranted it will stop being called.