Checkpoints
Part of the Roblox Game Scripting section of Coddy's Lua journey. Lesson 23 of 25.
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
MediumSave each player's progress at the glowing blue checkpoint.
- In
PlayerAdded, set the player'sRespawnLocationtoworkspace.Start. - When a player touches the checkpoint, set their
RespawnLocationtocheckpoint. - Then turn it green:
Color3.fromRGB(0, 255, 0).
Press Play, step on the checkpoint, then walk into the lava at the far end: you 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)
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