Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why sometimes my checkpoints system randomly skips the player level?

Asked by
Idleino 11
3 years ago

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)


1 answer

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago

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)
  • Don't use :connect with lowercase "c", it's deprecated
  • I recommend you using FindFirstChildOfClass which will only look for humanoids no matter what their name is, previously you could trick this by making a random part called "Humanoid" inside the character
  • Instead of looking for name of the character in Players, use GetPlayerFromCharacter function which is made exactly for that
  • Prefer using GetService instead of indexing them as game.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 though

Also 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:

ServerScriptService.CheckpointSystem

-- 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
0
how are you able to repost answers several times? JesseSong 3916 — 3y
1
holy no i can explain - i was in prague and scriptinghelpers is broken there so i was getting 504 & 502 errors when i tried to post my answer, at the end i gave up but all my attempts were actually posted lol imKirda 4491 — 3y
Ad

Answer this question