Why does my teleport script not work?

I want a script that makes so, when someone joins game A, they get teleported to game B. My game allows third party teleports and I have the following script in a non-local script in ServerScriptService, but it does nothing when I join game A, both on Roblox and Roblox Studio.

local Players = game:WaitForChild("Players")

local function SafeTeleport(TARGET_PLACE_ID, player)
	pcall(function()
		TeleportService:TeleportAsync(TARGET_PLACE_ID, {player})
	end)
end

local function onPlayerAdded(player)
	SafeTeleport(0, player) -- I had 0 replaced the ID of the game B's starter place.
end

Players.PlayerAdded:Connect(onPlayerAdded)

Nothing prints in the output. Can anyone help? Thank you!

1 Like

Nothing’s printing in the output because you’re catching it in the pcall without using any of the results.

Can you change that line of code to this and tell me what prints?

local success, result = pcall(TeleportService.TeleportAsync, TeleportService, TARGET_PLACE_ID, {player})
print(success, result)

This would cause a Cast To Value error, it should be:
TeleportService:TeleportAsync(target, {player})

This is the working script:

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

local function SafeTeleport(target, player)
	TeleportService:TeleportAsync(target, {player})
end

local function onPlayerAdded(player)
	local character = player.Character or player.CharacterAdded:Wait()
	character:WaitForChild("Humanoid") 
	-- pauses until they are for sure able to be teleported.
	local i = 0
	repeat
		task.wait()
		local S, E =  pcall(function()
			SafeTeleport(0, player)
		end)
		i += 1
	until S or i > 10
end

Players.PlayerAdded:Connect(onPlayerAdded)

So much for guessing …
ServerScript in ServerScriptService

local PlaceId = 123456789
local Players = game:GetService("Players")
local Portals = game:GetService("TeleportService")
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		Portals:Teleport(PlaceId, player)
	end)	
end)

Tested, and fast enough to cover a screen flash in the 1st game as it ports to the 2nd.
Async threw me off here. We are not passing data so it isn’t needed.

Sorry for late response :sweat_smile:

I’ve tried this script and it worked perfectly, and fast enough. Thank you all for helping @12345koip, @PandazDev & @2112Jay!

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