Prompting a player when he clicked a button

Hello,

So I want to make some developer products and when a player clicks a button then it will prompt him to buy the dev product.

I only came this far;

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

local productID = 
local player = game.Players.LocalPlayer

MarketPlaceService:PromptProductPurchase(player, productID)

Please tell me whats missing cause I can’t figure it out.

Help will be appreciated.

3 Likes

assuming you are talking about a GUI button here is what you would have to do:

local MarketPlaceService = game:GetService(“MarketplaceService”)
local Players = game:GetService(“Players”)
local player = Players.LocalPlayer
local Button = player.PlayerGui.URGUI.THEBUTTON
local productID =

Button.MouseButton1Down:Connect(function()
MarketPlaceService:PromptProductPurchase(player, productID)
end)

1 Like

I misread the topic whoops

@hockeylover29 Do call WaitForChild(), sometimes the Player’s Gui won’t exactly be replicated across the StarterGui as soon as the Player first joins

local MarketPlaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local PlayerGui = player:WaitForChild("PlayerGui")
local Button = PlayerGui:WaitForChild("GuiObject"):WaitForChild("ButtonToClick")
local productID = 3294239853982938498 --Idk I just put a random number

Button.MouseButton1Down:Connect(function()
    MarketPlaceService:PromptProductPurchase(player, productID)
end)
2 Likes

This works, but can i put this inside the function?

Cash.Value = Cash .Value + 50
1 Like

I know that there needs to be more stuff in but thats only for waht in the function needs to be.

1 Like

You could, but the only issue with that is that changing ObjectValues would only be replicated across your client and not to the server as well

You can also use an event detecting when the Player has finished purchasing a product by using the PromptProductFinished Event for the Server-Side to detect, so that you can change the Value from there that way

local MPS = game:GetService("MarketplaceService")

MPS.PromptProductFinished:Connect(function(UserIdOfPlayer, ProductID, WasPurchased)
    if WasPurchased then
        local Player = game.Players:GetPlayerByUserId(UserIdOfPlayer)

        if Player then
            Player.leaderstats.Cash.Value += 50
        end
    end
end)
1 Like

I will try that now i might respond later cause my studio is slow at this moment.

By the way, the developer hubs says a receipt call back does that needs to be in your gae=me?

1 Like

Where does this script needs to be in?

I believe you could put it as a ServerScript inside ServerScriptService? Not sure if the events will connect via replication though

I believe that’s only if you want to save the receipt in a DataStore so that you can keep track of it whenever a player purchases a certain item

this might work; put it in a serverscript in serverscriptservice

 local productFunctions = {}

    productFunctions[--productid] = function(receipt, player)
    local stats = player:FindFirstChild("leaderstats")
    local gold = stats and stats:FindFirstChild("Cash")
    if gold then
	gold.Value = gold.Value + 100

	return true
end


local function processReceipt(receiptInfo)


local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
local purchased = false
local success, errorMessage = pcall(function()
	purchased = purchaseHistoryStore:GetAsync(playerProductKey)
end)

if success and purchased then
	return Enum.ProductPurchaseDecision.PurchaseGranted
elseif not success then
	error("Data store error:" .. errorMessage)
end


local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then

	return Enum.ProductPurchaseDecision.NotProcessedYet
end


local handler = productFunctions[receiptInfo.ProductId]


local success, result = pcall(handler, receiptInfo, player)
if not success or not result then
	warn("Error occurred while processing a product purchase")
	print("\nProductId:", receiptInfo.ProductId)
	print("\nPlayer:", player)
	return Enum.ProductPurchaseDecision.NotProcessedYet
end


local success, errorMessage = pcall(function()
	purchaseHistoryStore:SetAsync(playerProductKey, true)
end)
if not success then
	error("Cannot save purchase data: " .. errorMessage)
end

return Enum.ProductPurchaseDecision.PurchaseGranted
end


 MarketplaceService.ProcessReceipt = processReceipt
1 Like

So, that wont be necassary then.

1 Like