Teleportation Script Help (Gamepass Related)

So, in my game, we have a VIP gamepass, and I wrote a script for it that is supposed to teleport the user to specific coordinates if you have the gamepass or have a group rank of 254+ (Dev+ in our group), but it doesn’t work. Does anyone have any ideas why?

local groupid = 12986119
local rank = 254
local MarketService = game:GetService(MarketplaceService)

script.Parent.Touched:Connect(function(hit)
    local function CheckForGamepass(Plr)
        local PlayerHasGamePass = MarketService:UserOwnsGamePassAsync(Plr.UserId, GamePassID)
        if PlayerHasGamePass == true then
        hit.Parent.HumanoidRootPart.Position = Vector3.new(-929.74, 35.884, -841.39)
        elseif PlayerHasGamePass == false then
            print("User does not have gamepass")
        elseif (hit and hit.Parent and game.Players:FindFirstChild(hit.Parent.Name)) then
            if (game.Players[hit.Parent.Name]:IsInGroup(groupid) and game.Players[hit.Parent.Name]:GetRankInGroup(groupid) >= rank) then
                hit.Parent.HumanoidRootPart.Position = Vector3.new(-929.74, 35.884, -841.39)
            else 
                print("Not in group")
            end
        end
    end
end)

I think the error is that you aren’t actually using the CheckForGamepass function. Try activating the function like this:

script.Parent.Touched:Connect(function(hit)
    local function CheckForGamepass(Plr)
        local PlayerHasGamePass = MarketService:UserOwnsGamePassAsync(Plr.UserId, GamePassID)
        if PlayerHasGamePass == true then
        hit.Parent.HumanoidRootPart.Position = Vector3.new(-929.74, 35.884, -841.39)
        elseif PlayerHasGamePass == false then
            print("User does not have gamepass")
        elseif (hit and hit.Parent and game.Players:FindFirstChild(hit.Parent.Name)) then

            if (game.Players[hit.Parent.Name]:IsInGroup(groupid) and game.Players[hit.Parent.Name]:GetRankInGroup(groupid) >= rank) then
                hit.Parent.HumanoidRootPart.Position = Vector3.new(-929.74, 35.884, -841.39)
            else 
                print("Not in group")
            end
        end
    end

 CheckForGamepass(<player>)
end)

You’re never calling the function you’ve created, so it will never run. Also, changing the position property of HumanoidRootPart will not change the position of the character, to teleport the character you can use Model:MoveTo or PVInstance:PivotTo. I also recommend checking if the instance that touched the part is actually a player.

local groupid = 12986119
local rank = 254
local MarketService = game:GetService(MarketplaceService)

script.Parent.Touched:Connect(function(hit)
    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)

if not Player then return end
    local function CheckForGamepass(Plr)
        local PlayerHasGamePass = MarketService:UserOwnsGamePassAsync(Plr.UserId, GamePassID)
        if PlayerHasGamePass == true then
        hit.Parent.HumanoidRootPart.CFrame = CFrame.new(-929.74, 35.884, -841.39)
        elseif PlayerHasGamePass == false then
            print("User does not have gamepass")
        elseif (hit and hit.Parent and game.Players:FindFirstChild(hit.Parent.Name)) then
            if (game.Players[hit.Parent.Name]:IsInGroup(groupid) and game.Players[hit.Parent.Name]:GetRankInGroup(groupid) >= rank) then
                hit.Parent.HumanoidRootPart.CFrame = CFrame.new(-929.74, 35.884, -841.39)
            else 
                print("Not in group")
            end
        end
    end

    CheckForGamepass(Player)
end)

You forgot to make the MarketPlaceService a string inside the GerService function, you also forget the GamePassID variable where it stores the gamepass id, I also recommend doing it like this!

local groupid = 12986119
local rank = 254
local MarketService = game:GetService("MarketplaceService")
local GamePassID = 00000000 -- change to your gamepass id

local function CheckForGamepass(plr)
	local PlayerHasGamePass = MarketService:UserOwnsGamePassAsync(plr.UserId, GamePassID)
	if PlayerHasGamePass == true then
		plr.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(-929.74, 35.884, -841.39)
	elseif PlayerHasGamePass == false then
		print("User does not have gamepass")
	elseif (game.Players:FindFirstChild(plr.Name)) then
		if (game.Players[plr.Name]:IsInGroup(groupid) and game.Players[plr.Name]:GetRankInGroup(groupid) >= rank) then
			plr.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(-929.74, 35.884, -841.39)
		else 
			print("Not in group")
		end
	end
end

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)

	if (player) then
		CheckForGamepass(player)
	end
end)

If this works please set as solution! :smiley:

1 Like

Honestly, I didn’t catch that; I just took the script and modified it from the OP. Thanks though! :slightly_smiling_face:

local players = game:GetService("Players")
local marketplace = game:GetService("MarketplaceService")
local userOwnsGamePass = marketplace.UserOwnsGamePassAsync

local part = script.Parent

local gamePassId = 0 --Change to ID of gamepass.
local groupId = 0 --Change to group's ID.
local groupRank = 0 --Change to group rank required.

part.Touched:Connect(function(hit)
	local hitModel = hit:FindFirstAncestorOfClass("Model")
	if hitModel then
		local hitPlayer = players:GetPlayerFromCharacter(hitModel)
		if hitPlayer then
			local success, result = pcall(userOwnsGamePass, marketplace, hitPlayer.UserId, gamePassId)
			if success then
				if result then
					hitModel:PivotTo(CFrame.new(-940, 35, 840))
					return
				end		
			else
				warn(result)
			end
			
			local success, result = pcall(function()
				return hitPlayer:IsInGroup(groupId)
			end)
			
			if success then
				if result then
					local success2, result2 = pcall(function()
						return hitPlayer:GetRankInGroup(groupId)
					end)
					
					if success2 then
						if result2 then
							if result2 >= groupRank then
								hitModel:PivotTo(CFrame.new(-940, 35, 840))
								return
							end
						end
					else
						warn(result2)
					end
				end
			else
				warn(result)
			end
		end
	end
end)

I tried this, but got the following error:

I replaced the “local GamePassID = 00000000” with the ID, but it did not work

Oh, this worked! I had to change the PivotTo coordinates though because I just got launched off the map, but thank you!

My bad, I rounded the numbers you were using and ended up with 840 instead of -840 for the CFrame’s Z component.

That’s alright, I had been trying to fix it for a bit, because as you can see, I’m a builder, and I only know Python so Lua is interesting, to say the least, so thank you! :upside_down_face: