Med Written By Bottrader

ProcessReceipt - DevProducts Created on: 06-01-2020

Once something is bought, their is an immediate outcome

Introduction:

To get an immediate outcome after a player has purchased a DevProduct, you need to know what ProcessReceipt is.

ProcessReceipt basically can tell if the purchase was successful or unsuccessful. If it is successful then you can make something happen right away whether it is giving them an item, or teleporting them somewhere, etc..!

Beginning:

To start this off, you first want to make sure you have a DevProduct ID. This DevProduct ID, we will put into a variable instead of having to memorize all those numbers. We also are gonna need one other variable, unless you are feeling extra. This variable is what is defining MarketplaceService. So in all, our variables will look like:

local DevProduct = 123456789
local MPS = game:GetService("MarketplaceService")

Now that we have all our variables defined, we need to start on the actual code. To begin this, we will need a function to take place. So this will contain parts of ProcessReceipt as that is what we are trying to get the game to realize we are doing.

function processReceipt(receiptInfo)

Now our function is ready to begin, but later we are going to have to close the function.

The next thing we are gonna do ( which is totally optional ) is define the player. This will help you be able to do something to the player when they do purchase the item. So to do this, we need get them by their UserId as we can't get them by their name in ProcessReceipt.

local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)

Now that we have it defined, we can easily access the player when we wanna do something.

After that, we can finally start to check if they purchased the DevProduct.

if receiptInfo.ProductId == DevProduct then

DevProduct is our variable we created earlier. Now it checks the receipt to see if the product ID purchased is the same as the DevProduct you are comparing it to.

If they are equal to each other, the script will continue going and at this point is when you will place your code for what you would like to happen to them. In this case, we are just gonna print that the player has purchased the item. In which our code now looks like:

local DevProduct = 123456789
local MPS = game:GetService("MarketplaceService")

function processReceipt(receiptInfo)
	local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if receiptInfo.ProductId == DevProduct then
		print(player.Name.." has purchased this developer product!")
	end
end

Returning Information:

The next step is to return the information of the purchase granted. To do this we are going look at the decision they had, to see if they did purchase it or not. You also don't want this to happen in the same code as the checking of IDs. So, you want to put this code inbetween the ends.

return Enum.ProductPurchaseDecision.PurchaseGranted

Now we are about ready to finish the script.

Closing the function:

Closing functions are always really easy to do. All we really have to do for this is go into MarketplaceService and make the receipt there equal to the one that was created when the player purchased the item. You can simply do this by:

MPS.ProcessReceipt = processReceipt

We completed the code part, and we ended up getting:

local Market = game:GetService("MarketplaceService")
local ID = 944308420

function processReceipt(receiptInfo)
	local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if receiptInfo.ProductId == ID then
		print(player.Name.." has purchased this developer product!")
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted
end



Market.ProcessReceipt = processReceipt

Conclusion:

We made it where if a player bought a developer product, it would print that they bought. Of course, you can always add whatever you want to happen there, but for now that is how I made it. I hope this helped you out!

Med Written By Bottrader

See Bottrader's profile on Roblox

Discussion