Prompting to buy a Paid Place

I’m trying to make a teleport that teleports you if you bought access to a game, if you don’t have access it will prompt you to buy it.

For some reason it won’t teleport you or prompt you to buy it.

Here is the script I tried making:

local gameID = 72339019
local MKS = game:GetService("MarketplaceService")






local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local PlayerOwnsAsset = MarketplaceService.PlayerOwnsAsset
 
script.Parent.Touched:Connect(function(hit)
  local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    local success, doesPlayerOwnAsset = pcall(PlayerOwnsAsset, MarketplaceService, plr, gameID)
    if doesPlayerOwnAsset then
        
        
        game:GetService("TeleportService"):Teleport(gameID, plr) ---Teleports the player.

       
        
        
        else
    
   
        MKS:PromptPurchase(plr, gameID) --- Prompts it.
        
        
    end

These are the 2 errors in the output:


Screen Shot 2020-08-21 at 9.00.21 AM
If you see anything wrong, please let me know.

Hey! Please try to format your code better in the future it will make it easier for others to read, the issue if that your not validating if the Hit is actually a player, so you need to check if the plr variable is not equal to nil or true, also made a few other changes:

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

local GAME_ID = 72339019

script.Parent.Touched:Connect(function(hit)
    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
    
    if (Player) then
        local Success, DoesPlayerOwnAsset = pcall(MarketplaceService.PlayerOwnsAsset, MarketplaceService, Player, GAME_ID)
        
        if (DoesPlayerOwnAsset) then
            TeleportService:Teleport(GAME_ID , Player)
        else
            MarketplaceService:PromptPurchase(Player, GAME_ID) 
        end
    end
end)

Ensure this script is parented to the part the player should touch, thanks @Blokav

1 Like

@cjjdawg pointed this out already but you want to do a check that the part that touched the teleporter actually belongs to the player. That is likely what was causing the ‘Invalid player to teleport’ error.

As for the ‘Touched is not a valid member of Workspace’ error, can you make sure that this script has the teleporting part as it’s parent? From the context of the error it looks like the script is a direct child of the Workspace.

1 Like

Off topic but related: You should also do a check on your paid place, exploiters may teleport.

1 Like

In addition, make sure to also utilize the Success value returned by the pcall function. When the function runs normally, Success would be set to true and DoesPlayerOwnAsset is whether the player has game access.

If the function errors, Success would be false, and DoesPlayerOwnAsset would be the error message, so by checking the truthiness of DoesPlayerOwnAsset it’s still going to attempt to teleport the player even if the function call failed. You should do something like this:

(modified cjjdawg’s code a little)

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

local GAME_ID = 72339019

script.Parent.Touched:Connect(function(hit)
    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
    
    if (Player) then
        local Success, DoesPlayerOwnAsset = pcall(MarketplaceService.PlayerOwnsAsset, MarketplaceService, Player, GAME_ID)
        if (Success) then
            if (DoesPlayerOwnAsset) then
                TeleportService:Teleport(GAME_ID , Player)
            else
                MarketplaceService:PromptPurchase(Player, GAME_ID) 
            end
        else
            warn("Check failed:", DoesPlayerOwnAsset)
        end
    end
end)

@Blokav @cjjdawg Thank you for the help, sadly, both of your scripts didn’t work. I’m starting too think that it’s not possible to teleport someone to a paid game.

It is, just there may be some errors.