Can someone explain dev products a bit for me

So I’ve got this script to handle my products. But I don’t really understand how the receipt function works. I assume that there is a way to track all purchases of x player. Like if I wanted to track purchases from a player named “Bob101” how would I do that? Thank you.

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local productFunctions = {}

-- buy 100 stars
productFunctions[1504961992] = function(receipt, player)
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		print("Give stars")
		player.PlayerData.Stars.Value += 100
		-- Indicate a successful purchase
		return true
	end
end



local function processReceipt(receiptInfo)
	local userId = receiptInfo.PlayerId
	local productId = receiptInfo.ProductId

	local player = Players:GetPlayerByUserId(userId)
	if player then
		-- Get the handler function associated with the developer product ID and attempt to run it
		local handler = productFunctions[productId]
		local success, result = pcall(handler, receiptInfo, player)
		if success then
			-- The player has received their benefits!
			-- return PurchaseGranted to confirm the transaction.
			return Enum.ProductPurchaseDecision.PurchaseGranted
		else
			warn("Failed to process receipt:", receiptInfo, result)
		end
	end

	-- the player's benefits couldn't be awarded.
	-- return NotProcessedYet to try again next time the player joins.
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

-- Set the callback; this can only be done once by one script on the server!
MarketplaceService.ProcessReceipt = processReceipt



ProcessReceipt fires when a player clicked to accept buy from prompt, it contains a ReceiptInfo table.

When that event happens, you have this function to handle the process processReceipt(receiptInfo) which receives the Receipt with the info.
The info contained inside the Receipt:
image

Then by using the ProductID which is in the ReceiptInfo, you have a table of functions, which will perform a specific task for that ProductID, this one:
(This is what you want to happen when player bought the product, example, you want to give something to the player, in your case you are giving 100 stars)

productFunctions[1504961992] = function(receipt, player)
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		print("Give stars")
		player.PlayerData.Stars.Value += 100
		-- Indicate a successful purchase
		return true
	end
end

If that function ran correctly, and returned true to the pcall(), Means that you gave the 100 stars to player succesfully, then its time to tell to roblox that player got the product in your game successfully. Thats why you will run this line: Enum.ProductPurchaseDecision.PurchaseGranted
That line is to tell Roblox the purchase was done correctly.

If your function to give the starts went wrong, then this line will run:
Enum.ProductPurchaseDecision.NotProcessedYet
Which mean the purchase still pending on your side. So Roblox will try again to complete the purchase the next time player joins or when possible.

Thats all.
Is very important to use PurchaseGranted and NotProcessedYet to keep Roblox informed about your sales. You better read carefully the documentation, if you do it incorrectly could even look like you are scamming in your game.

I dont think thats necesary, but if you want to do that just after this line use it:

local userId = receiptInfo.PlayerId
if game.Players:GetNameFromUserIdAsync(userId) == "Bob101" then

(use a pcall for that maybe)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.