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)
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.
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