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

How would i make a player teleport to a checkpoint when they die?

Asked by 4 years ago

So, when the player dies, I want them to go back to the checkpoint they touched. I used a code and i don't know what the problem is and I am a bit confused on how to get this working. Any ideas on how? Here is my script I Wrote:

local checkFolder = game.Workspace.checkpoints

game.Players.PlayerAdded:Connect(function(plr) --// just defining the folder leaderstats
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local checkpoints = Instance.new("IntValue") --// making a value in a folder called checkpoints
    checkpoints.Name = "Stages"
    checkpoints.Parent = leaderstats

    for i,v in pairs(checkFolder:GetChildren()) do
        if v:IsA("BasePart") then
            v.Touched:Connect(function(plr)
                local humanoid = plr.Parent:FindFirstChild("Humanoid")
                if i == 1 then
                    checkpoints.Value = 1
                    humanoid.Died:Connect(function()
                        plr:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(v.Position + Vector3.new(0,2,0)) --// this is where i get confused as it doesn't work. Any ideas?
                    end)
                end
            end)
        end
    end
end)

I tried using CFrame and made it the position of the checkpoint plus 2 on the "y" axis. The For i,v in pairs part is finding all children in the folder checkFolder. Please help me

0
try characteradded instead of died Arkrei 389 — 4y
0
Ok i will kingblaze_1000 359 — 4y
0
It doesn't work :( Could you please explain this further? Do i have to use a parameter for this? kingblaze_1000 359 — 4y
0
Use "if humanoid.Health == 0 then" matiss112233 258 — 4y
0
I tried that but it didn't work kingblaze_1000 359 — 4y

1 answer

Log in to vote
0
Answered by
Lakodex 711 Moderation Voter
4 years ago
Edited 4 years ago

Here. It was simple...

local checkFolder = game.Workspace.checkpoints

game.Players.PlayerAdded:Connect(function(plr) --// just defining the folder leaderstats
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local checkpoints = Instance.new("IntValue") --// making a value in a folder called checkpoints
    checkpoints.Name = "Stages"
    checkpoints.Parent = leaderstats

    for i,v in pairs(checkFolder:GetChildren()) do
        if v:IsA("BasePart") then
            v.Touched:Connect(function(plr)
        local char = plr.Character or plr.CharacterAdded:Wait() --// Didn't add this to wait for the character
                local humanoid = char.Parent:WaitForChild("Humanoid") --// You said "FindFirstChild". If the player was not loaded I'd automatically cancel itself and return an error.
                if i == 1 then
                    checkpoints.Value = 1
                    humanoid.Died:Connect(function()
                        char:WaitForChild("HumanoidRootPart").CFrame = v.CFrame * CFrame.new(0, 3, 0) --// Part should be fixed 
                end
            end)
        end
    end
end)
0
But, now when the player dies, they go straight to the checkpoint and then when their character is fixed, they will go to spawn. Please any way to stop this? kingblaze_1000 359 — 4y
Ad

Answer this question