Menu
CoddyTech

Teleport Players

Lesson 6 of 20 in Coddy's Roblox Game: Disaster Survival course.

An island nobody stands on is no game. When a round starts, every player goes to the map's Spawn, and when it ends, back to the lobby.

Players:GetPlayers() lists everyone in the game, and a player's body is player.Character, a Model. Model:PivotTo(cframe) moves a whole model at once, every part keeping its place. A CFrame is a position plus a rotation, and adding a Vector3 moves it:

local function teleport(player, target)
	local character = player.Character
	if character then
		character:PivotTo(target.CFrame + Vector3.new(0, 4, 0))
	end
end

The 4 studs up drop the player a little above the target instead of inside it. player.Character is nil for a moment after joining or dying, hence the if.

challenge icon

Challenge

Medium

Send the players to the island and back.

  1. Write teleport(player, target): if player.Character exists, PivotTo(target.CFrame + Vector3.new(0, 4, 0)).
  2. Right after the map is loaded, teleport every player to map.Spawn.
  3. When the round's countdown ends, teleport everyone to lobbySpawn, before the map is destroyed.

Press Play: you land on the island when the round starts and back in the lobby when it ends. Then Check.

Try it yourself

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

print("Rounds last " .. Settings.RoundLength .. " seconds")

-- Every player gets Wins on the leaderboard
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
end)

-- The status board in the lobby
local gui = Instance.new("SurfaceGui")
gui.Parent = workspace.StatusBoard

local status = Instance.new("TextLabel")
status.Name = "Status"
status.Size = UDim2.fromScale(1, 1)
status.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
status.TextColor3 = Color3.fromRGB(255, 255, 255)
status.TextScaled = true
status.Text = "Welcome to Disaster Survival!"
status.Parent = gui

local function setStatus(text)
	status.Text = text
end

-- Lesson 6: Teleport Players goes here

-- The round loop
while true do
	for seconds = Settings.Intermission, 1, -1 do
		setStatus("Intermission: " .. seconds)
		task.wait(1)
	end

	local map = ServerStorage.Maps.Island:Clone()
	map.Name = "Map"
	map.Parent = workspace

	for seconds = Settings.RoundLength, 1, -1 do
		setStatus("Time left: " .. seconds)
		task.wait(1)
	end

	map:Destroy()
end

All lessons in Roblox Game: Disaster Survival

Practice on your own: Roblox playground