Trying to find HumanoidRootPart Gives Error

Hey there!

function teleportPlayers(player)
	
	for i, player in ipairs(Players:GetPlayers()) do
		
		if player then
			
			local character = player:FindFirstChild('Character')
			print('found character')
			local root = character:FindFirstChild("HumanoidRootPart")
			print('found root part')
			root.Position = selectedMap.Position
			print('moved hrp')
			
		end
		
	end
	
end

The print statement ‘found character’ runs fine, but nothing after that works. This appears in the output:

It references this line in the above code:

local root = character:FindFirstChild("HumanoidRootPart")

If you need any more information please just let me know. Thanks for any and all help :slight_smile:.

2 Likes

As your error says, your character is nil.

This row doesn’t actually guarantee that you find the character. I would instead use Player.Character (roblox.com) to find the character.

2 Likes

character doesnt exist inside the player, just do local character = player.Character

try doing
if player.Character and player.Character.HumanoidRootPart then
–tp
end
and dont use :FindFirstChild as it finds a child and character is a property

Thanks for the reply, I appreciate it. It worked fine!

1 Like

Hello,

  1. Character and Player can be with the same name and belong to the same guy, however, neither of them is the child / parent of the other. Each of them has different stuff that you can apply on.

2)You can do something like this:

local character = game.Workspace:FindFirstChild(player.Name)

“Character” isn’t a child of the player it’s a property which points to/refers to the player’s character model.

To index this property you can do the following.

local character = player.Character

2 Likes

You could also add or player.CharacterAdded:Wait() , if it hasnt loaded yet

Could result in the for loop yielding indefinitely if the player’s character is never added.

I’d recommend adding if instance then at each step though, to ensure each instance exists before attempting to index it.

Yeah, I agree with that. :+1:t4: Best wishes