Skinstashery
Roblox
Home / Roblox / Studio script library / Checkpoint system
RB-004

Roblox checkpoint system script

Players respawn at the last checkpoint part they touched, with a Stage counter.

Script type
Script
Where it goes
ServerScriptService
Category
Game flow

For your own game in Roblox Studio. Not for use in other people's games.

The code

-- Checkpoint system for an obby.
-- Put your checkpoint Parts in a Folder in the Workspace named "Checkpoints".
-- Name them 1, 2, 3 ... in the order players reach them.

local FOLDER_NAME = "Checkpoints"
local SPAWN_HEIGHT = 4        -- studs above the checkpoint to place the player

local Players = game:GetService("Players")

local folder = workspace:WaitForChild(FOLDER_NAME)

local function getStage(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if not leaderstats then
		leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player
	end

	local stage = leaderstats:FindFirstChild("Stage")
	if not stage then
		stage = Instance.new("IntValue")
		stage.Name = "Stage"
		stage.Value = 0
		stage.Parent = leaderstats
	end
	return stage
end

local function onPlayerAdded(player)
	local stage = getStage(player)

	player.CharacterAdded:Connect(function(character)
		if stage.Value == 0 then
			return -- no checkpoint reached yet; use the normal spawn
		end

		local checkpoint = folder:FindFirstChild(tostring(stage.Value))
		local root = character:WaitForChild("HumanoidRootPart", 5)
		if checkpoint and root then
			character:PivotTo(checkpoint.CFrame + Vector3.new(0, checkpoint.Size.Y / 2 + SPAWN_HEIGHT, 0))
		end
	end)
end

Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in Players:GetPlayers() do
	task.spawn(onPlayerAdded, player)
end

-- Set up each checkpoint.
for _, checkpoint in folder:GetChildren() do
	local number = tonumber(checkpoint.Name)
	if number and checkpoint:IsA("BasePart") then
		checkpoint.Touched:Connect(function(hit)
			local player = Players:GetPlayerFromCharacter(hit.Parent)
			if not player then
				return
			end
			local stage = getStage(player)
			if number > stage.Value then
				stage.Value = number
			end
		end)
	end
end

How to set it up

  1. In the Workspace, add a Folder and name it Checkpoints.
  2. Put your checkpoint Parts inside the folder. Rename them 1, 2, 3 and so on. Set Anchored to true.
  3. Add the Script to ServerScriptService.
  4. Play. Touch checkpoint 2, then reset your character. You should come back at checkpoint 2.

Change it

More in Game flow

All Roblox scripts

More Roblox tools