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!
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.