Teleport still happens after the player cancels the purchase

Hi, I’m trying to make a part that when stepped on triggers a developer product prompt, if this developer product is bought then the player will get teleported

I managed to make a script that does exactly that, but there’s a problem

The player can just cancel the purchase and will get teleported regardless

Here’s the script

local triggerPart = script.Parent
local developerProductId = 1718617521
local skippedPart = workspace:WaitForChild("Skipped")

local function onTouch(hit)
    local character = hit.Parent
    local humanoid = character:FindFirstChildOfClass("Humanoid")

    if humanoid then
        local player = game.Players:GetPlayerFromCharacter(character)

        if player then
            game:GetService("MarketplaceService"):PromptProductPurchase(player, developerProductId)

            local connection
            connection = game:GetService("MarketplaceService").PromptProductPurchaseFinished:Connect(function(purchaseSuccess)
                connection:Disconnect()
                if purchaseSuccess then
                    character:SetPrimaryPartCFrame(CFrame.new(skippedPart.Position))
                else
                    warn("Purchase failed or was canceled.")
                end
            end)
        else
            warn("Could not find player instance.")
        end
    end
end

triggerPart.Touched:Connect(onTouch)


If it wasn’t clear already, I want the teleport not to happen if the purchase is canceled, I’ve tried everything but nothing works lol

Thats because the first variable it returns is the UserId as shown in the documentation

Instead try this code:

local triggerPart = script.Parent
local developerProductId = 1718617521
local skippedPart = workspace:WaitForChild("Skipped")

local function onTouch(hit)
    local character = hit.Parent
    local humanoid = character:FindFirstChildOfClass("Humanoid")

    if humanoid then
        local player = game.Players:GetPlayerFromCharacter(character)

        if player then
            game:GetService("MarketplaceService"):PromptProductPurchase(player, developerProductId)

            local connection
            connection = game:GetService("MarketplaceService").PromptProductPurchaseFinished:Connect(function(userId,assetId,purchaseSuccess)
                connection:Disconnect()
                if purchaseSuccess then
                    character:SetPrimaryPartCFrame(CFrame.new(skippedPart.Position))
                else
                    warn("Purchase failed or was canceled.")
                end
            end)
        else
            warn("Could not find player instance.")
        end
    end
end

triggerPart.Touched:Connect(onTouch)
1 Like