How do I teleport players?

I want to teleport randomPlayer to GameAreaSpawn in my game:

local Status = game.ReplicatedStorage.Status
local StatusTitle = game.ReplicatedStorage.StatusTitle
local Mazes = game.ServerStorage.Mazes:GetChildren()
local maze = Mazes[math.random(1, #Mazes)]:Clone()
local GameAreaSpawn = game.Workspace.GameArea.GameAreaSpawn

local Players = game:GetService("Players"):GetPlayers()
local randomPlayer = Players[math.random(1, #Players)]

local function startIntermission()
	StatusTitle.Value = "Intermission"
	for i = 5, 0, -1 do
		wait(1)
		Status.Value = i
	end
end

local function startGame()
	wait(1)
	StatusTitle.Value = "Game"
	for i = 5, 0, -1 do
		Status.Value = i
		wait(1)
	end
end

startIntermission()	
maze.Parent = game.Workspace
-- Teleport random player here
startGame()

How can I do it?

1 Like

I’m not sure, but maybe this will work:

local Character = randomPlayer.Character or randomPlayer.CharacterAdded:Wait()
Character:MoveTo(GameAreaSpawn.Position)

You’ll have to loop through the players, like this!

for Index, Player in ipairs(Players) do -- Loops through the players 1 by 1.
    Player.Character:MoveTo(GameAreaSpawn.Position) -- Moves the character to the given Position.
end

You can’t move Player, you’ll first have to get it’s Character, and then move the character, but that’ll work too. Also I’m not sure if he wants to move all Players, or just 1 random Player.

Whoops! I’ll update the code, thanks for pointing that out!

:MoveTo uses Vector3, switch that to :PivotTo and it will work.
image

:MoveTo is good, as long as you use Instance’s Position, not the CFrame.

I know, @CommanderRanking used a CFrame though, which is why I thought they meant to use :PivotTo because CFrames aren’t compatible with :MoveTo.

I’m so used to using HumanoidRootPart, I’ll also change that.

1 Like
--//Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local Status = ReplicatedStorage.Status
local StatusTitle = ReplicatedStorage.StatusTitle
local Mazes = ServerStorage.Mazes:GetChildren()
local maze = Mazes:GetChildren()[math.random(1, #Mazes)]:Clone()
local GameAreaSpawn = workspace.GameArea.GameAreaSpawn

local Players = Players:GetPlayers()
local randomPlayer = Players[math.random(1, #Players)]

--//Functions
local function startIntermission()
	StatusTitle.Value = "Intermission"
	for i = 5, 0, -1 do
		task.wait(1)
		Status.Value = i
	end
end

local function startGame()
	task.wait(1)
	StatusTitle.Value = "Game"
	for i = 5, 0, -1 do
		Status.Value = i
		task.wait(1)
	end
end

--//Initialization
startIntermission()	
maze.Parent = game.Workspace
randomPlayer.Character:MoveTo(GameAreaSpawn.Position)
startGame()

I get a weird error:
17:31:51.303 ServerScriptService.Main:8: invalid argument #2 to 'random' (interval is empty) - Server - Main:8

Switch the line that’s erroring to this:

local maze = Mazes:GetChildren()[math.random(1, #Mazes)]:Clone()

It’s actually erroring to line 14:
local randomPlayer = Players[math.random(1, #Players)]

Try deleting #, maybe that’ll work.

Try this:

--//Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local Status = ReplicatedStorage.Status
local StatusTitle = ReplicatedStorage.StatusTitle
local Mazes = ServerStorage.Mazes:GetChildren()
local maze = Mazes[math.random(1, #Mazes)]:Clone()
local GameAreaSpawn = workspace.GameArea.GameAreaSpawn

local PlayerTable = Players:GetPlayers()
local randomPlayer = PlayerTable[math.random(1, #PlayerTable)]

--//Functions
local function startIntermission()
	StatusTitle.Value = "Intermission"
	for i = 5, 0, -1 do
		task.wait(1)
		Status.Value = i
	end
end

local function startGame()
	task.wait(1)
	StatusTitle.Value = "Game"
	for i = 5, 0, -1 do
		Status.Value = i
		task.wait(1)
	end
end

--//Initialization
startIntermission()	
maze.Parent = game.Workspace
randomPlayer.Character:MoveTo(GameAreaSpawn.Position)
startGame()

Personally I use :SetPrimaryPartCFrame(), but I’ve heard that it got deprecated so I wouldn’t recommend it any longer if another method works.