How to make this thanks for your purchase gui work properly?

Recently, I made a script in my game where if the player purchases an item, a “Thanks for your purchase” UI comes up. It works fine except for one issue, the UI still comes up after I click cancel on the purchase UI that comes up when buying the dev product. How can I make it so that the UI only comes up when the player clicks the buy button

Here is the script currently:

local MPS = game:GetService("MarketplaceService")

MPS.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == 1151090217 then
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Levels.Value = player.leaderstats.Levels.Value + 20
		return Enum.ProductPurchaseDecision.PurchaseGranted

	elseif receiptInfo.ProductId == 1151090304 then
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Rebirths.Value = player.leaderstats.Rebirths.Value + 1
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
	elseif receiptInfo.ProductId == 1161224953 then
		local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		player.leaderstats.Gems.Value = player.leaderstats.Gems.Value + 10
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

Explanations would be highly appreciated :grinning:

1 Like
local MarketplaceService = game:GetService("MarketplaceService") -- Calling the MPS class
local Players = game:GetService("Players") -- Calling Players class



MarketplaceService.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == ID then
		local player = Players:GetPlayerByUserId(receiptInfo.PlayerId) -- Since receiptInfo holds PlayerId, we can collect it and use it for the Level's distribution
		if player then 
            player.leaderstats.Levels.Value += 20
			return Enum.ProductPurchaseDecision.PurchaseGranted
		else
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
	elseif  receiptInfo.ProductId == ID then
		local player = Players:GetPlayerByUserId(receiptInfo.PlayerId) -- Since receiptInfo holds PlayerId, we can collect it and use it for the Level's distribution
		if player then 
            player.leaderstats.Levels.Value += 20
			return Enum.ProductPurchaseDecision.PurchaseGranted
		else
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
	end
end

Edit oh ye forgot to add
This is a server script, if you want to send a thank you message to client use remote events.

Thank you for your answer, works perfectly.

2 Likes