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)
Instead of checking for the bought variable I did: if marketplaceService:PlayerOwnsAsset(game:GetService("Players"):GetPlayerByUserId(PlayerId), id) then
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
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