Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

5 comments

0

try characteradded instead of died

0

Ok i will

0

It doesn't work :( Could you please explain this further? Do i have to use a parameter for this?

0

Use "if humanoid.Health == 0 then"

0

I tried that but it didn't work

1 answer

✓ Accepted answer

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)

1 comment

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?

This discussion is preserved as it was. New answers, comments, and votes are closed.