Hi. I'm currently making an obby and i found out that sometimes if i die it would make me skip a level any ideas why?
This is the checkpoint system:
script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then if game.Players:FindFirstChild(hit.Parent.Name) then local Player = game.Players:FindFirstChild(hit.Parent.Name) local Stage = Player:WaitForChild("leaderstats"):WaitForChild("Stage") if Stage.Value == script.Parent.Name -1 then Stage.Value = script.Parent.Name end end end end)
That can be because your body parts are touching the checkpoint, your script does not include any checking if the touched player is dead or not. Logically the player is dead if humanoid's health is lower than 0, you can use the method below.
script.Parent.Touched:Connect(function(hit) local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid") if Humanoid and Humanoid.Health > 0 then local Player = game.Players:GetPlayerFromCharacter(hit.Parent) if Player then local Stage = Player:WaitForChild("leaderstats"):WaitForChild("Stage") if Stage.Value == script.Parent.Name - 1 then Stage.Value = script.Parent.Name end end end end)
:connect
with lowercase "c", it's deprecatedPlayers
, use GetPlayerFromCharacter function which is made exactly for thatgame.Service
, if you rename the service you want to get, the second method won't work. This happens very rarely, it's still a good practice thoughAlso notice how you put the same script into all checkpoints, if you want to make a little change you must modify each of them, you can avoid that by putting checkpoints into their specific folder and creating a script which will apply same functions to them, this is done using ipairs, here is an example of how you can make it work:
-- Folder which contains all the checkpoints local Checkpoints = game.Workspace.Checkpoints for _, Checkpoint in ipairs(Checkpoints:GetChildren()) do Checkpoint.Touched:Connect(function(hit) local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid") if Humanoid and Humanoid.Health > 0 then local Player = game.Players:GetPlayerFromCharacter(hit.Parent) if Player then local Stage = Player:WaitForChild("leaderstats"):WaitForChild("Stage") if Stage.Value == script.Parent.Name - 1 then Stage.Value = script.Parent.Name end end end end) end