Does using .PromptProductPurchaseFinished for processing DevProduct Okay?

Hi So I am Currently making a Revive System where Player can pay Devproduct to revive himself, or to revive His teammate.

Here is the code on ServerScript for the revive.

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

Players.PlayerAdded:Connect(function(player)
	repeat task.wait() until player and (player.Character or player.CharacterAdded:Wait()) and player.Character:FindFirstChild("Humanoid")
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	
	humanoid.HealthChanged:Connect(function(health)
		--Code to make people downed and create proximity
		
		proximityPrompt.Triggered:Connect(function(plr)
			MarketplaceService:PromptProductPurchase(plr, 123)
			MarketplaceService.PromptProductPurchaseFinished:Connect(function(userId, productId, isPurchased)
				if productId ~= 123 or not isPurchased then return end
				
				-- code to revive player using the variables of humanoid and playerInstance previously defined here
			
			end)
		end)
	end)
end)

Here is the ProcessReceipt Script

local MarketplaceService = game:GetService("MarketplaceService")

productFunctions[123] = function(receipt, player)
    return true
end

local function processReceipt(receiptInfo)
	local userId = receiptInfo.PlayerId
	local productId = receiptInfo.ProductId

	local player = Players:GetPlayerByUserId(userId)
	if player then
		-- Get the handler function associated with the developer product ID and attempt to run it
		local handler = productFunctions[productId]
		local success, result = pcall(handler, receiptInfo, player)
		if success then
			-- The user has received their benefits
			-- Return "PurchaseGranted" to confirm the transaction
			return Enum.ProductPurchaseDecision.PurchaseGranted
		else
			warn("Failed to process receipt:", receiptInfo, result)
		end
	end

	-- The user's benefits couldn't be awarded
	-- Return "NotProcessedYet" to try again next time the user joins
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

-- Set the callback; this can only be done once by one server-side script
MarketplaceService.ProcessReceipt = processReceipt

The reason I am using .PromptProductPurchaseFinished is because so that It can use the previously defined humanoid & ProximityPrompt variable In the revive script to revive other player or the person that bought the DevProduct.

Is this method efficient or is there better alternative to make this? Thanks

1 Like

define humanoid in process receipt, and ur making a memory leak by connecting to Triggered everything a humanoids health changes

How would I define it if for example a player buyed a revive for another player that is down?

I’d assume you’re making a GUI on which player to revive, what I would do is make a table like this

you’d fire the remote everytime they select a new player to revive

this should give you a general idea on what to do

local tbl = {}

-- playeradded

tbl[player] = player.Name -- // define the username to revive

-- define a remote to update this

--[[ OnServerEvent connect (player, otherPlayer) -> {
	tbl[player] = otherPlayer.Name
}
]]
-- processreciept

local function processReceipt(receiptInfo)
	local userId = receiptInfo.PlayerId
	local productId = receiptInfo.ProductId

	local player = Players:GetPlayerByUserId(userId)
	if player then
		-- Get the handler function associated with the developer product ID and attempt to run it
		local playerToRevive = Players:FindFirstChild(tbl[player]) -- // this could be the same player
		local handler = productFunctions[productId]
		local success, result = pcall(handler, receiptInfo, player)
		if success then
			-- The user has received their benefits
			-- Return "PurchaseGranted" to confirm the transaction
			return Enum.ProductPurchaseDecision.PurchaseGranted
		else
			warn("Failed to process receipt:", receiptInfo, result)
		end
	end

	-- The user's benefits couldn't be awarded
	-- Return "NotProcessedYet" to try again next time the user joins
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

-- Set the callback; this can only be done once by one server-side script
MarketplaceService.ProcessReceipt = processReceipt

-- playerremoving
tbl[player] = nil
2 Likes