Check if player purchased using proximity prompt

I’ve been trying to make a proximity prompt purchase thing but it isn’t working very well… I’m trying to give a player an object if they purchased the devproduct.

Script

local devproduct = game.ServerStorage.currency
local cola = game.ReplicatedStorage.BloxyCola
local prompt = workspace.Part.ProximityPrompt

local cola_clone = cola:Clone()

if prompt.Triggered then
	game.MarketplaceService.ProcessReceipt = function(receiptInfo, player)
		local plr = game.Players:GetPlyerByUserId(receiptInfo.PlayerId)
		if receiptInfo.ProductId == devproduct.Value then
			cola_clone.Parent = plr
			plr:WaitForChild("spent")
			currency.Value = currency.Value + 15
			print("Bought")
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
	end
end
1 Like

Here is a basic script I used for one of my games. It seems you did not check when the ProductPurchaseDecision was processed.

local marketplaceService = game:GetService("MarketplaceService")
local proximityprompt = script.Parent

proximityprompt.Triggered:Connect(function(plr)
	marketplaceService:PromptProductPurchase(plr, productId)
	local function completePurchase()
		if plr then
			-- your code here
			return Enum.ProductPurchaseDecision.PurchaseGranted
		elseif not plr then
 			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
	end
	marketplaceService.ProcessReceipt = completePurchase
end)

You’re going to need to make use of datastores if you want developer product purchases to be remembered such that the prompt can be retriggered allowing the player to receive the aforementioned “object” again.

Unless you want the purchase to be prompted each time the player triggers the proximity prompt.

local devproduct = game.ServerStorage.currency
local cola = game.ReplicatedStorage.BloxyCola
local prompt = workspace.Part.ProximityPrompt

prompt.Triggered:Connect(function(player)
	game.MarketplaceService:PromptProductPurchase(player, devproduct.Value)
end)

game.MarketplaceService.ProcessReceipt = function(receiptInfo, player)
	if receiptInfo.ProductId == devproduct.Value then
		local plr = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		if plr then
			cola:Clone().Parent = plr
			plr.leaderstats.currency.Value = plr.leaderstats.currency.Value + 15
			return Enum.ProductPurchaseDecision.PurchaseGranted
		else
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
	end
end
1 Like

I’ve tried this but I got an error saying Unable to cast value to Object on game.MarketplaceService:PromptProductPurchase(player.UserId, devproduct)

There was a minor typo, try now.

One last problem I tried the script and adjusted it a bit and when I bought the actual thing, there was an error saying GetPlyerByUserId is not a valid member of Players "Players" - Server - DevProducts:13

That bit was from your original script GetPlyerByUserId needs an a, so it should be GetPlayerByUserId instead.

https://developer.roblox.com/en-us/api-reference/function/Players/GetPlayerByUserId