Give player item after buying Developer Product

I want to give players after buying a Developer Product a specific item in their backpack.
I’ve got the basics of the Developer Product script, but I am not sure how to add the lines which actually gives the item to the player.

This is what I already got:

local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
 
-- Data store for tracking purchases that were successfully processed
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")
 
-- Table setup containing product IDs and functions for handling purchases
local productFunctions = {}
productFunctions[946322487] = function(receipt, player)
--What do I have to add here to give the player his or her item?
		-- Indicate a successful purchase
		return true
	end
end

-- The core 'ProcessReceipt' callback function
local function processReceipt(receiptInfo)
 
	-- Determine if the product was already granted by checking the data store  
	local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
	local purchased = false
	local success, errorMessage = pcall(function()
		purchased = purchaseHistoryStore:GetAsync(playerProductKey)
	end)
	-- If purchase was recorded, the product was already granted
	if success and purchased then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	elseif not success then
		error("Data store error:" .. errorMessage)
	end
 
	-- Find the player who made the purchase in the server
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		-- The player probably left the game
		-- If they come back, the callback will be called again
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	-- Look up handler function from 'productFunctions' table above
	local handler = productFunctions[receiptInfo.ProductId]
 
	-- Call the handler function and catch any errors
	local success, result = pcall(handler, receiptInfo, player)
	if not success or not result then
		warn("Error occurred while processing a product purchase")
		print("\nProductId:", receiptInfo.ProductId)
		print("\nPlayer:", player)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
 
	-- Record transaction in data store so it isn't granted again
	local success, errorMessage = pcall(function()
		purchaseHistoryStore:SetAsync(playerProductKey, true)
	end)
	if not success then
		error("Cannot save purchase data: " .. errorMessage)
	end
 
	-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
	return Enum.ProductPurchaseDecision.PurchaseGranted
end
 
-- Set the callback; this can only be done once by one script on the server! 
MarketplaceService.ProcessReceipt = processReceipt
4 Likes

On the line where it returns or says that the product has been successfully bought what you can do is go into the place where the item is located and :Clone the tool in the players backpack like this;

Tool:Clone().Parent = Players.LocalPlayer.Backpack

but this will only work in a localscript so if this is a normal script then I suggest you use

:FireClient

to give the player the tool with what I mentioned above.

Don’t do either of these. ReceiptInfo passes the purchasing player’s id and the OP already fetches the player using this id and the function GetPlayerById, so all that you would need to do is to parent the tool to the player’s backpack. Simple.

function(receipt, player)
    local tool = somewhereAVariableForATool:Clone()
    tool.Parent = player:WaitForChild("Backpack")
    return true
end
1 Like

Should this be added in a completely new script?