How to ensure I waited for the part

Hi!

I am creating a checkpoint system and whenever they die, they are suppose to respawn back at their checkpoint. However, this doesn’t work as the line where it FindFirstChild(“HumanoidRootPart”) doesn’t correctly get the humanoid root part.

This therefore makes an error later in my code as the HRP doesn’t exist. So the CFrame index returns nil.

The weird part is, though, if I change it to :WaitForChild(), it says infinite yield possible.

How do I make sure it gets the HRP cleanly without any issues? Thank you!

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")
			print("found hrp and the death event fired")
			for i, v in ipairs(workspace:GetDescendants()) do
				if v:FindFirstChild("Decal") then
					print('decal')
					if v:FindFirstChild("Decal").Texture == "rbxasset://textures/SpawnLocation.png" then
						print('spawnpoint')
						print(plr.Team, plr.Team.Name, v.Name)
						if plr.Team.Name == v.Name then
							print("it found the checkpoint")
							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)
						end
					end
				end
			end
		end)
	end)
end)

Check if the humanoidrootpart exists, and only if it does the code should do its thing.

 if char:FindFirstChild("HumanoidRootPart") then
{
			local hrp = char:FindFirstChild("HumanoidRootPart")
			print("found hrp and the death event fired")
			for i, v in ipairs(workspace:GetDescendants()) do
				if v:FindFirstChild("Decal") then
					print('decal')
					if v:FindFirstChild("Decal").Texture == "rbxasset://textures/SpawnLocation.png" then
						print('spawnpoint')
						print(plr.Team, plr.Team.Name, v.Name)
						if plr.Team.Name == v.Name then
							print("it found the checkpoint")
							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)
						end
					end
				end
			end
}

Hey! I fixed it using another method, but thank you so much for replying. I truly appreciate it. :smile:

For anyone in the future who has the same problem, please refer to this post!!

1 Like