Character Won't Teleport when Using Character Added Event

Hi, when messing around with some scripts, I found a weird error in my code. When the events fires, it doesn’t teleport the player to location. The script does not show any errors.

game.Players.PlayerAdded:Connect(function()
   plr.CharacterAdded:Connect(function(character)
		local HumanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		
		if HumanoidRootPart then
			HumanoidRootPart.CFrame = CFrame.new(Vector3.new(1, 15, 1))
		end
   end)
end)

The expected result is the character gets teleported to the cframe showed. If anyone knows why this happens please let me know, thanks.

1 Like

This script does work as intended but you need to add a wait in either in the if statement or before.

The reason for this is because the character loads in, it teleports itself back to (0,0,0) or whatever the spawn plate is at. If you have a tiny delay like wait(0.1) the character loads in and then is teleported after it is spawned.

	if HumanoidRootPart then
wait(0.1)
			HumanoidRootPart.CFrame = CFrame.new(Vector3.new(1, 15, 1))
		end
3 Likes

It’s not working because you are trying to find the HumanoidRootPart before it is created. Replace FindFirstChild with WaitForChild or wait until the character is a descendant of the workspace and then create the variable.

		if not character:IsDescendantOf(workspace) then
			repeat
				task.wait()
			until character:IsDescendantOf(workspace)
		end

		local HumanoidRootPart = character:FindFirstChild("HumanoidRootPart")
1 Like

You forgot the ‘Player’ parameter of PlayerAdded.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Root = Character:WaitForChild("HumanoidRootPart")
		task.wait()
		Root.CFrame = CFrame.new(1, 15, 1)
	end)
end)

WaitForChild() should be used (in this instance) and a short yield is necessary to allow for loading.

2 Likes

This seems to work 90% of the time. Is there any way I can make this more reliable?

I’m having this issue, but my character falls over every time I teleport them.

If you dont want character ragdolls, you can turn them off also try not to necro post.

Character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
Character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)