Checkpoints
Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 22 of 24.
When a player is defeated, Roblox waits 5 seconds (Players.RespawnTime) and gives them a new character on a SpawnLocation. If the place has several SpawnLocations, Roblox picks one at random, so a checkpoint can turn into a starting point by accident.
Every player has a RespawnLocation property. Set it to a SpawnLocation and that player always respawns there. Obbies use it twice: when a player joins, it points at the start; when they reach a checkpoint, it moves to the checkpoint.
local Players = game:GetService("Players")
local camp = workspace.Camp
camp.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
player.RespawnLocation = camp
end
end)RespawnLocation belongs to each player, so one player reaching the camp does not move anyone else.
Challenge
MediumThe gray pad is workspace.Start, and the glowing blue pad is a checkpoint (both are SpawnLocations). This script is inside the checkpoint.
- When a player joins, set their
RespawnLocationtoworkspace.Start. - When a player touches the checkpoint, set their
RespawnLocationto the checkpoint and turn it green:Color3.fromRGB(0, 255, 0).
Press Play, step on the checkpoint, then walk into the lava at the far end: you should come back on the checkpoint. Then Check.
Try it yourself
local Players = game:GetService("Players")
local checkpoint = script.Parent
Players.PlayerAdded:Connect(function(player)
-- Start every player at workspace.Start
end)
checkpoint.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
-- Respawn this player here from now on
-- Turn the checkpoint green
end
end)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Roblox Game Scripting
2Loops and Functions
Row of PillarsStaircase BuilderTower BuilderCoin FactoryBlinking LightVanishing Bridge5Your First Game
Your First ObbyPractice on your own: Online Lua compiler