Click to prompt purchase

I want to prompt up a developer product purchase when a player clicks a block.

I already got the part of the purchase itself but I am not sure how to script the part with ClickDetector.
local sword = game.ServerStorage.Sword
local mpService = game:getService("MarketplaceService")

mpService.ProcessReceipt = function(purchaseInfo)
    local plr = game:getService("Players"):GetPlayerByUserId(purchaseInfo.PlayerId)
    if purchaseInfo.ProductId == 49078483 then
        local Sword = sword:Clone()
        Sword.Parent = plr.Backpack
    end
    return Enum.ProductPurchaseDecision.PurchaseGranted
end

How should I script that?

This could have easily been solved by searching on the web, because I found this article that solves the problem, but all you simply need to do is use something called PromptProductPurchase(). You can add it in like this:

 local productId = 0000000 -- id of dev product
 local mps = game:GetService("MarketplaceService")
 script.Parent.ClickDetector.MouseClick:Connect(function(player) -- grabs the player parameter
     mps:PromptProductPurchase(player, productId) -- Prompts the purchase and makes sure to save it in the player
 end)

Make sure this script is parented to the object

3 Likes

Would it be a LocalScript, because it deals with one player? Or a regular Script?

Ok I am very sorry, when it comes to promptpurchase my brain goes stupid, it doesn’t matter which script you put it in, but in this case do a serverscript.

1 Like

I think that I did something wrong. Should I put the ClickDetector script in a seperate script?

I am busy with searching for how to get a script working for months, but I keep failing every time.

Update: I’ve put your script in a LocalScript inside the part but it still doesn’t work.

Scripting is one of my worst skills, and I’ve read alot of articles the Developer Hub. I still don’t understand it. Do I have to paste script 1 in the RemoteEvent in the ReplicatedStorage?

And do I have to put script 2 in a a LocalScript and put it in StarterPlayerScripts?

You made a small mistake in your code, you dont need to pass the UserId with the promptProductPurchase function. You need to pass the player itself.

local sword = game.ServerStorage.Sword
local mpService = game:getService("MarketplaceService")

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	mpService:PromptProductPurchase(player, 946322487)
end)

mpService.ProcessReceipt = function(purchaseInfo)
    local plr = game:getService("Players"):GetPlayerByUserId(purchaseInfo.PlayerId)
    if purchaseInfo.ProductId == 946322487 then
        local Sword = sword:Clone()
        Sword.Parent = plr.Backpack
    end
   	return Enum.ProductPurchaseDecision.PurchaseGranted
end
1 Like

If this helps, I have made a video on this for you @MeesBean (Also sent this to you in our little discussion). And this video can also be for anybody else who needs help on this:

3 Likes