Teleport Players
Lesson 6 of 18 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
endThe 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
MediumMove the players:
- Near the top, keep the lobby's spawn:
local lobbySpawn = workspace.LobbySpawn. - Write
teleport(player, target)as in the lesson. - Right after the map is loaded, teleport every player to
map.Spawn. - When the round's countdown ends, teleport every player to
lobbySpawn, before the map is destroyed.
Press Play and ride along, 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
-- 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.