Giving the player an item after purchasing a developer product

I’m trying to make the player receive an item after purchasing a developer product. I’m confused on how check if the player actually buys the product so that they can recieve their item.

Local Script:

local event = game:GetService("ReplicatedStorage").RemoteEvents.BoomboxPurchased
local player = game.Players.LocalPlayer

local marketplaceService = game:GetService("MarketplaceService")

local id = 3377333242

local bought = false --dont know how to check if the player actually bought the item


script.Parent.MouseButton1Click:Connect(function()
	marketplaceService:PromptProductPurchase(game:GetService("Players").LocalPlayer, id)
	marketplaceService:PromptProductPurchase(player, id)

end)

marketplaceService.PromptProductPurchaseFinished:Connect(function(PlayerId, IsPurchased)
	print("purchase check")
	if bought then
		print("firing server")
	event:FireServer()
	else
		print("purchase failed")
	end
end)

The docs have a good example on how to implement dev products.

I don’t usually explore with that service but try this

 marketPlaceService:PlayerOwnsAsset(game:GetService("Players"):GetPlayerByUserId(PlayerId), id)

Instead of checking for the bought variable I did:
if marketplaceService:PlayerOwnsAsset(game:GetService("Players"):GetPlayerByUserId(PlayerId), id) then

Console says it didn’t work

  13:33:17.229  purchase check  -  Client - LocalScript:18
  13:33:17.343  purchase failed  -  Client - LocalScript:23

Ok let me find a solution. Ok!

1 Like

I guess you need to set it up like this?

productFunctions[id] = function(receipt, player)
	-- Do functionality here
	
	return true 
end

marketplaceService.ProcessReceipt = function(receiptInfo)
	local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	local handler = productFunctions[receiptInfo.ProductId]
	if handler then
		local success = handler(receiptInfo, player)
		if success then
			return Enum.ProductPurchaseDecision.PurchaseGranted
		end
	end
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

What would I define productFunctions as?

Probably a ‘ModuleScript’ in ‘ReplicatedStorage’ or where you want to put it.
I’d define it as a table storing purchases–from different id’s–Code:

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

local productFunctions = require(game.ReplicatedStorage.ProductFunctions)

marketplaceService.ProcessReceipt = function(receiptInfo)
    local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
    if not player then
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end
    
    local handler = productFunctions[receiptInfo.ProductId]
    if handler then
        handler(player)
    end

    return Enum.ProductPurchaseDecision.PurchaseGranted
end

I’m confused; is the code in the ModuleScript running each of the purchased items? For example, if I buy a boombox, the script for the boombox is in the ModuleScript.

Also should I be handling the purchase in a Server Script or a Local Script?

If its the ServerScript I could fire the event as soon as the prompt is closed and then the Server handles the purchase and fires another event that the client would pick up so the Server Script can give the player the item.

Oh wait I think you can just do this.
Local Script:

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local productId = 3377333242

script.Parent.MouseButton1Click:Connect(function()
	MarketplaceService:PromptProductPurchase(player, productId)
end)

Server Script:

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

local productId = 3377333242

local function processReceipt(receiptInfo)
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	if receiptInfo.ProductId == productId then
		print(player.Name .. " bought the Boombox!")

		local boombox = game.ServerStorage:WaitForChild("Boombox"):Clone()
		boombox.Parent = player.Backpack
	end

	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketplaceService.ProcessReceipt = processReceipt
1 Like

oh wait your script is different my bad

Yes! But you can fire the event if you want. If needed.

1 Like

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