How to check if a developer product purchase was made?

local TextButton = script.Parent
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local destination = game.Workspace.dest.Position
local productId = 1815441724

local function teleportPlayer()
	character:PivotTo(CFrame.new(destination))
	print("Teleported player")
end

local function handlePurchaseFinished(productId, wasPurchased)
	print("Purchase finished event fired")
	if wasPurchased then
		teleportPlayer()
		print("Product purchased, so teleported")
	else
		print("Product not purchased, did not teleport player")
	end
end

local function onClick()
	MarketplaceService:PromptProductPurchase(player, productId)
	print("Prompted purchase for product with ID:", productId)
end

MarketplaceService.PromptProductPurchaseFinished:Connect(function(productId, wasPurchased)
	handlePurchaseFinished(productId, wasPurchased)
end)
TextButton.Activated:Connect(onClick)

I’m having an issue where purchase checks are not working properly. I think my logic here may be wrong. If the user cancels the purchase prompt, it’s counted as a successful purchase so the player teleports which is not what I want, the player should not teleport. The condition where they purchase the product works fine though. So, what I’m trying to say is, cancelling and purchasing results in teleports (successful purchase case).

Also if I say if was purchased == true then in both situations, it’s counted as an unsuccessful purchase, so teleportation does not occur.

I’m not a programmer, I made it using AI.

it should be like his

MarketplaceService.PromptProductPurchaseFinished:Connect(function(playerID,productId, wasPurchased)
	handlePurchaseFinished(playerID,productId, wasPurchased)
end)

it takes 3 arg like that. And the reason it was successful cause was purchased storing the product ID as it was suppose to the 2nd arg.

like this

local function handlePurchaseFinished(playerID,productId, wasPurchased)
	print("Purchase finished event fired")
	if wasPurchased then
		teleportPlayer()
		print("Product purchased, so teleported")
	else
		print("Product not purchased, did not teleport player")
	end
end

[/quote]

1 Like

yes and make sure to modify the handlePurchaseFinished function so it got the three arguments

1 Like

Thank you @OneXZero_DEV and @fraiseFR004 I did what you both said, my issue is addressed.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.