How to know if a player has bought a developer product

I was making a script that would, once bought, give a player +100 coins. For some reason, it won’t work for me. It will prompt me to buy the developer product, but when I test buy it, it doesn’t do anything. Here is the script. (The parent of the script is a TextButton)

local id = "1006738178"
local localplayer = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
	game.MarketplaceService:PromptProductPurchase(localplayer, id)
	if Enum.ProductPurchaseDecision.PurchaseGranted == true then
		game.Players.LocalPlayer.leaderstats.Coinn.Value = game.Players.LocalPlayer.leaderstats.Coinn.Value + 1
	elseif Enum.ProductPurchaseDecision.PurchaseGranted == false then
		game.Players.LocalPlayer.leaderstats.Coinn.Value = game.Players.LocalPlayer.leaderstats.Coinn.Value + 0
	end
	end)

Please tell me what is wrong with this script and how to fix it. Thanks!

3 Likes

I’m not the best with DevProducts but heres what I used in one of my projects

local MarketPlaceService = game:GetService("MarketplaceService")
local function processReceipt(receiptInfo)
	if receiptInfo.ProductId == 'productID' then
		local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	player.leaderstats.'leaderstat'.Value = player.leaderstats.'leaderstat'.Value + 'Value'
	return Enum.ProductPurchaseDecision.PurchaseGranted
end
MarketPlaceService.ProcessReceipt = processReceipt

Hope this could help you!

3 Likes

I tried your script, but now it won’t prompt me to buy the item. Why is this?

Edit: Nevermind, I found out why. Trying again right now

1 Like

I forgot, in the button you must add a LocalScript

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

local productId ='productid'
script.Parent.MouseButton1Down:Connect(function(promptPurchase)
	local player = Players.LocalPlayer
	MarketPlaceService:PromptProductPurchase(player, productId)
end)

This doesn’t give the player the currency, only prompts the purchase. The other code controls the if purchased part

EDIT: Have this script in a LocalScript inside the button
and Have the other one somewhere in ServerScriptService

1 Like

Okay, but when I put the 1st script you typed in the ServerScriptService, am I supposed to put that in a LocalScript? Because you had a local value that was local player = game.Players.LocalPlayer, and only LocalScript can have LocalPlayer.

Here is the script that I have right now inside ServerScriptService under a regular script:

local player = game.Players.LocalPlayer
local productId =  1006738178
local function processReceipt(receiptInfo)
	if receiptInfo.ProductId == 1006738178 then
		local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	player.leaderstats.Coinn.Value = player.leaderstats.Coinn.Value + 100
	return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	end

Here’s the one that I have in the TextButton under a LocalScript:

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

local productId =  1006738178
script.Parent.MouseButton1Down:Connect(function(promptPurchase)
	local player = Players.LocalPlayer
	MarketPlaceService:PromptProductPurchase(player, productId)
end)

In the server script you must get the MarketPlaceService

1 Like

Do you mean just add local MarketPlaceService = game:GetService("MarketplaceService") to the script in serverscriptservice? If so, this did not work for me.

1 Like

So do I put this in ServerScriptService or in the LocalScript inside of the actual TextButton?

I defined player from playerUderId

1 Like

Developer Products are handled using a Callback. That callback is MarketPlaceService.ProcessReceipt(). That callback is supposed to return Enum.ProductPurchaseDecision.PurchaseGranted when the purchase is granted and if it has not yet been granted return Enum.ProductPurchaseDecision.NotProcessedYet

2 Likes

Your script did not work for me. I tried modifying it a bit, but it was no use–

local MarketPlaceService = game:GetService("MarketplaceService")
local player = game:GetService("Players").LocalPlayer
local productId = 1006738178
local assetId = 1006738178
script.Parent.MouseButton1Down:Connect(function()
	MarketPlaceService:PromptProductPurchase(player, productId)
end)
game.MarketplaceService.PromptPurchaseFinished:Connect(function(player, assetId, ispurchased)
    if ispurchased then
         player.leaderstats.Coinn.Value = player.leaderstats.Coinn.Value + 100
    else
         player.leaderstats.Coinn.Value = player.leaderstats.Coinn.Value + 0
    end
end)

How would I use MarketPlaceService.ProcessReceipt()?

You need to change it to your own function

1 Like

Just to confirm, it is supposed to look like this, right?

game.MarketplaceService.promptproductpurchasefinished:Connect(function(player, assetId, ispurchased)

You are not supposed to use PromptProductPurchaseFinished for handling purchases. This function is commonly used only for seeing if the player has bought or did not buy the developer product. You are to use ProcessReceipt.

The developer hub page documenting PromptProductPurchaseFinished advises you to not use this function for handling purchases

3 Likes

Using ProcessReceipt is the correct way to do it. Since the prompt won’t dissapear if you don’t use ProcessReceipt

1 Like

Tried it again, didn’t work. It looked like this:

game.MarketplaceService.PromptProductPurchaseFinished:Connect(function(player, assetId, ispurchased)

I’m sorry, @LukaDev_0, I’m a bit new to scripting, and have no clue about how to use processReciept. Should I do something like this?

MarketPlaceService.ProcessReceipt()
if Enum.ProductPurchaseDecision.PurchaseGranted then
	player.leaderstats.Coinn.Value = player.leaderstats.Coinn + 100
elseif Enum.ProductPurchaseDecision.NotProcessedYet then
	return 
end

Why are you using an if statement? ur supposed to return a value
Example:

reffer to @Rare_tendo’s example

2 Likes