A friend and I are currently working on a small obby to test our Lua scripting skills and we're currently working on a checkpoint system that will respawn your character at the latest checkpoint you were at.
I have a part below the obby level that teleports you back to the checkpoint if you fall but when you run into a kill part it teleports you back you level one. I've tried all sorts of ways of trying to fix this error. I've gotten pretty close to almost solving it but can't seem to get any further with it.
This is what I'm using to teleport a player back when they fall and it works like a charm.
-- VARIABLES local Players = game:GetService("Players") local fallRespawnPart = workspace.Map.Barriers:FindFirstChild("respawn") -- IF PLAYER HIT THE RESPAWN BARRIER , TELEPORT THEM 2 STUDS ABOVE THE LAST CHECK POINT fallRespawnPart.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then local plr = Players:GetPlayerFromCharacter(hit.Parent) local respawnPoint = workspace:FindFirstChild("Checkpoints"):FindFirstChild(tostring(plr.leaderstats.Stages.Value)) plr.RespawnLocation = respawnPoint plr.Character.HumanoidRootPart.Position = respawnPoint.Position + Vector3.new(0, 2, 0) end end)
This is how far I got with checking if the player has respawned and if so teleport them back to the checkpoint but it seems to always teleport back to the start of the obby. If you need any more details I'm happy to give it
-- Player Enters Game Players.PlayerAdded:Connect(function(player) -- CHECK FOR CHARACTER RESPAWN player.CharacterAdded:Connect(function(character) -- GRAB LOCAL PLAYER local plr = Players:GetPlayerFromCharacter(character) -- SET MOST RECENT CHECKPOINT FROM LEADERSTATS VALUE local respawnPoint = workspace:FindFirstChild("Checkpoints"):FindFirstChild(tostring(plr.leaderstats.Stages.Value)) -- SET SPAWN LOCATION plr.RespawnLocation = respawnPoint -- RESPAWN HUMANOID AT LOCATION WITH 2 STUDS ABOVE GROUND plr.Character.HumanoidRootPart.Position = respawnPoint.Position + Vector3.new(0, 2, 0) end) end)
Checkpoint Tree:
Workspace --> Checkpoints --> [1, 2, 3, 4] <-- These are the names of my spawnlocations
The problem was in my respawn player at checkpoint script aka the 2nd code block in the original post I was Respawning the player with plr.Character.HumanoidRootPart.Position = respawnPoint.Position + Vector3.new(0, 2, 0)
After I asked a friend he told me I should have a wait loop until the character is not nil
repeat wait() until character ~= nil
I then Switched the original line that spawned you at the position to this.
character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(respawnPoint.Position + Vector3.new(0, 2, 0))
Taking out plr at the beginning and waiting for the HumanoidRootPart to detect before respawning me at the last checkpoint I visited
In total, this is the complete block of code with no comments
Players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) repeat wait() until character ~= nil local respawnPoint = workspace["Checkpoints"]:FindFirstChild(tostring(player.leaderstats.Stages.Value)) player.RespawnLocation = respawnPoint character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(respawnPoint.Position + Vector3.new(0, 2, 0)) end) end)