Respawning people at their most recent checkpoint that they've touched

I’m having trouble making it so that when they touch a previous checkpoint they respawn at that stage instead of the stage that they’ve gotten too so far. The first script is the one we’re working with because this will send the player to checkpoint that they’ve recently touched. The other scripts are just in-case you need more info.

A piece of the main script in the game:

	player.CharacterAdded:Connect(function(character)
		repeat wait() until workspace:FindFirstChild(character.Name)

		local player = game.Players:GetPlayerFromCharacter(character)
		local checkpoint = cpFolder[player.leaderstats.Checkpoint.Value]

		character.HumanoidRootPart.CFrame = checkpoint.CFrame + Vector3.new(0,2,0)

	end)
	
end)

The stage handler:

local checkpoints = workspace:WaitForChild("Checkpoints")


for i,v in pairs(checkpoints:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				
				if tonumber(v.Name) > player.leaderstats.Checkpoint.Value then
					player.leaderstats.Checkpoint.Value = tonumber(v.Name)
				end
				
			end
		end)
	end
end


Teleport handler:

local remoteevent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

local teleportpart = workspace.Objects:WaitForChild("Teleport")

remoteevent.OnServerEvent:Connect(function(player)

local humanoidrootpart = player.Character:WaitForChild("HumanoidRootPart")

humanoidrootpart.CFrame = teleportpart.CFrame + Vector3.new(0,4,0)

end)

Still can’t find a solution, I’ve been trying for quite some time now.

Your question is too convoluted and is probably being passed-up by would-be helpers. Try clarifying and simplifying your question and code posts.

It’s a word-wall right now for what may actually be a simple issue.

Updated it, thanks for the feedback!

Thanks. Just remove the if statement around this line then. That if statement blocks them from going back to the previous stage.

Unless you mean you want them to keep their current progress on the leaderboard but change the spawn?

1 Like

Yep, I mean when they touch a previous checkpoint they spawn there instead of at the farthest stage they’ve gotten to.

You need to introduce a new, hidden, leaderstat then (if you want it to save). Otherwise, store another IntValue in the player. Call it SpawnPoint.

When they touch a checkpoint, change both the leaderstat and the hidden value. However, only change the leaderstat if they touch a checkpoint that is greater than the current value; but change the SpawnPoint no matter what they touched.

Only save the leaderstat and not the SpawnPoint. So if they rejoin their SpawnPoint is reset back the leaderstat.

1 Like

I really don’t see the purpose of this, since you already put it in character added :joy:

try something more like this after the Character Added

local checkpoint = cpFolder[player.leaderstats.Checkpoint.Value]
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
HumanoidRootPart.CFrame = checkpoint.CFrame + Vector3.new(0,2,0)
2 Likes

Must’ve done something wrong because it isn’t working. cpfolder is the list of checkpoints, then I took the current IntValue which is a counter which determines which level you’re at. SpawnPoint should be pretty self explanatory since you’ve already stated it.

	player.CharacterAdded:Connect(function(character)
		repeat wait() until workspace:FindFirstChild(character.Name)

		local player = game.Players:GetPlayerFromCharacter(character)
		local respawn = cpFolder[game.StarterGui.Level.Current.Value]

		character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,2,0)

	end)
	
end)

AND:

local checkpoints = workspace:WaitForChild("Checkpoints")


for i,v in pairs(checkpoints:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				
				if tonumber(v.Name) > player.leaderstats.Checkpoint.Value then
					player.leaderstats.Checkpoint.Value = tonumber(v.Name)
					game.StarterGui.Level.SpawnPoint.Value = game.StarterGui.Level.Current.Value
				end
				
			end
		end)
	end
end