Currently you can get any checkpoint, whether it's behind you, or 5 stages ahead of you, I want to make it so that only the next stage's checkpoint can be gotten. Here is my checkpoint script in ServerScriptService:
local Players = game:GetService('Players') Players.PlayerAdded:Connect(function(player) local stage = player:WaitForChild("leaderstats"):WaitForChild('Stage') player.CharacterAdded:Connect(function(character) local checkpoint = game.Workspace.Stages:FindFirstChild(stage.Value) delay(.01, function() character:SetPrimaryPartCFrame(checkpoint.CFrame + Vector3.new(0,10,0)) end) end) end)
And here is the checkpoint script that goes in with my stages in the "Stages" folder:
for i,v in pairs(script.Parent:GetChildren()) do if v:isA("Part") then v.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Humanoid").Health >= 1 and v.Name ~= "0" then game.Players:FindFirstChild(hit.Parent.Name).leaderstats.Stage.Value = v.Name end end) end end
for i,v in pairs(script.Parent:GetChildren()) do if v:isA("Part") then v.Touched:Connect(function(hit) local character = hit.Parent local player = game.Players:GetPlayerFromCharacter(character) if player then local touchedstage = tonumber(v.Name) if touchedstage == (player.leaderstats.Stage.Value + 1) then player.leaderstats.Stage.Value = touchedstage end end end) end end
I can't tell you exactly where to put this. Or exactly how your own code works (I'm new and never worked with checkpoint systems), but you might want to add and attribute to the checkpoints (click "add attribute" in the properties tab) and make a Boolean value. Name it "isTouched", and set it to false.
Now, add this line of code, when the player hits the checkpoints and saves data there:
game.Workspace.CheckpointTest.isTouched == true
Now, when you detect the hit, you want to add this with the word "and"
game.Workspace.CheckpointTest.Touched:Connect(function(hit) if hit:FindFirstChild:("Humanoid") then if game.Workspace.CheckpointTest.isTouched == true then --tp player back to other checkpoint end end end)