Won't teleport after death

Hi,

I am making a system where the player gets teleported to their checkpoint after dying. However, it just simply does not teleport, and I have no idea why. No errors in the output.

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local checkpointValue = plr.Character:FindFirstChild("CheckpointValue")
		
		plr.Character:WaitForChild("Humanoid").Died:connect(function()
			local checkpoints = workspace:GetDescendants()
			local hrp = char:FindFirstChild("HumanoidRootPart")
			for i, v in ipairs(workspace:GetDescendants()) do
				if v:FindFirstChild("Decal") then
					if v:FindFirstChild("Decal").Texture == "rbxasset://textures/SpawnLocation.png" then
						if v:FindFirstChild("CheckpointValue").Value == checkpointValue then
							if hrp then
								repeat
									hrp.CFrame = v.CFrame + Vector3.new(0, 2, 0) -- Teleport 2 studs above the checkpoint
								until hrp.CFrame == v.CFrame + Vector3.new(0, 2, 0)
								print("Teleported upon death team for player " .. plr.Name .. ": " .. v.Name)
							else
								warn("HumanoidRootPart not found for " .. plr.Name)
							end
						end
					end
				end
			end
		end)
	end)
end)
1 Like

I normally don’t spoonfeed but i am extremely bored, could you test this?

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(newChar)
		local checkpoints = workspace:GetDescendants()
		local hrp = newChar.PrimaryPart
		for i, v in ipairs(workspace:GetDescendants()) do
			if v:FindFirstChild("Decal") and
			  v:FindFirstChild("Decal").Texture == "rbxasset://textures/SpawnLocation.png" and
			  v:FindFirstChild("CheckpointValue").Value == checkpointValue and
			  hrp
			then
				hrp.CFrame = v.CFrame + Vector3.new(0,2,0)
				print("Teleported upon death team for player " .. plr.Name .. ": " .. v.Name)
				return
			else
				warn("HumanoidRootPart not found for " .. plr.Name)
			end
		end
	end)
end)

Simply wait for the character to respawn, then teleport to the position!
(make sure the checkpoint system is working)

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local checkpointValue = plr.Character:FindFirstChild("CheckpointValue")
		
		plr.Character:WaitForChild("Humanoid").Died:connect(function()
			local checkpoints = workspace:GetDescendants()
			local hrp = char:FindFirstChild("HumanoidRootPart")
			for i, v in ipairs(workspace:GetDescendants()) do
				if v:FindFirstChild("Decal") then
					if v:FindFirstChild("Decal").Texture == "rbxasset://textures/SpawnLocation.png" then
						if v:FindFirstChild("CheckpointValue").Value == checkpointValue then
							if hrp then
                                repeat 
                                    wait(0.2)
                                until hrp.Parent.Humanoid.Health > -5
                                warn('Player could be teleported!')
								repeat
									hrp.CFrame = v.CFrame + Vector3.new(0, 2, 0) -- Teleport 2 studs above the checkpoint
								until hrp.CFrame == v.CFrame + Vector3.new(0, 2, 0)
								print("Teleported upon death team for player " .. plr.Name .. ": " .. v.Name)
							else
								warn("HumanoidRootPart not found for " .. plr.Name)
							end
						end
					end
				end
			end
		end)
	end)
end)

The way I went about this is making each checkpoint a SpawnLocation and turning off CharacterAutoLoads in the Players service. When the player touches a checkpoint, their RespawnLocation property is set to that spawn. That way, you don’t have to deal with teleporting them. I manually respawn the player using LoadCharacter() after setting the RespawnLocation.

However, you have to do a magnitude check and teleport them back in case the respawning doesn’t work.

Hi, thank you @eyado2122_1 and @McGamerNick for replying!! Unfortunately both of these do not work, and I simply do not have any idea for you eyado.

McGamerNick there was a problem in your code. In the if statement it says and hrp, and idk what you were tryna do there

Could you show us how your checkpoint instance looks like?

local function teleportToCheckpoint(plr: Player)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local checkpointValue = plr:FindFirstChild("CheckpointValue")
	local checkpoints = workspace:GetDescendants()
	local hrp = char.PrimaryPart
	for i, instance in workspace:GetDescendants() do
		if instance:FindFirstChild("Decal") then
			
			if instance:FindFirstChild("Decal").Texture == "rbxasset://textures/SpawnLocation.png" then
				if instance:FindFirstChild("CheckpointValue").Value == checkpointValue.Value then
					if hrp then
						char:PivotTo(instance.CFrame + Vector3.new(0,2,0))
						
						print("Teleported upon death team for player " .. plr.Name .. ": " .. instance.Name)
						break
					else
						warn("HumanoidRootPart not found for " .. plr.Name)
					end
				end
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	local CheckpointValue = Instance.new("IntValue")
	CheckpointValue.Name = "CheckpointValue"
	CheckpointValue.Value = 1
	CheckpointValue.Parent = plr
	
	teleportToCheckpoint(plr)
	plr.CharacterAdded:Connect(function(char)
		teleportToCheckpoint(plr)
	end)
end)

Here is my code and below will be a video of proof it works.

How it works

  • This code works by connecting the player added event then creating the checkpoint value inside of their PLAYER not the character. So it doesn’t reset.
  • calls the function to teleport the player to their Checkpoint Value
  • Inside the function we do our basic character variables and the PLAYER’S CheckpointValue
  • Do a for loop of every descendant (as you game suggests) then checks if it matches our checkpoint instance properties. If it does then we pivot the character’s model to the instance.CFrame + Vector3.new(0,2,0) to position it 2 studs above. Then we break out of the function for performance sake.

Tips

I would suggest making all your checkpoints inside one folder in the workspace. Like game.Workspace.Checkpoints this way you can do for a loop of each Children in that folder this will heavily improve your checkpoint loading time and cause less stress on the server.

You can also decide to use CollectionService which allows for Tagging Instances this way you can just do CollectionService:HasTag(instance, "Checkpoint") and that will check if it is an instance of a checkpoint. This way you don’t have to do many if statements of the Decal, if the instance has a CheckpointValue

A minor nit pick would be to opt into using Attributes for the instances instead of using IntValues. Attributes do the same thing as an IntValue but its the standard for roblox developer norms! This way whenever you want to change a Checkpoint you can just adjust that value rather than adjusting the IntValue. I don’t know if there is any performance differences but I assume Attribute would be faster as its not an actual Instance In The Game if someone can benchmark this or teach me how to benchmark this, it would be amazing to know!

Anyways if you have any further questions please let me know as I can assist you if you want to go with the more performant, and get your game up to norms.

(P.S) you could always sell it in the future if someone is willing to buy it :smiley:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.