Can't teleport a character after player.CharacterAdded, parent is nil?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want to teleport a player’s character to a location right after they spawn

  2. What is the issue?
    It won’t work for whatever reason. I printed the character’s parent after it spawns and it says nil?

  3. What solutions have you tried so far?
    I had a problem with “attempt to index nil with SetPrimaryPartCFrame” so I used player.CharacterAdded, this seems to have a problem as well.

local players = Players:GetPlayers() --Get the players
	for index, player in ipairs(players) do --Atempt to teleport the two players' characters
		local character = player.Character
		if not character then --If character hasn't loaded
			character = player.CharacterAdded:Wait() --Wait until it has
			print(character,character.Parent) -- this prints "(Character Name, nil)
		end
		if index == 1 then --Just teleporting them
			character:SetPrimaryPartCFrame(CFrame.lookAt(Vector3.new(0,5,40),Vector3.new(0,0,0)))
		elseif index == 2 then
			character:SetPrimaryPartCFrame(CFrame.lookAt(Vifctor3.new(0,5,-40),Vector3.new(0,0,0)))
		end
	end

If the character hasn’t loaded before the check, then the parent is nil. How do I fix this? It also doesn’t give an error when I try to teleport the orphan character, it just does nothing and gives no error. Any Ideas? I’ve already tried

player.CharacterAppearanceLoaded:Wait() 

as well.

are you trying to get two random players?

why do you care about the character’s parent? (which should be workspace, but why?)

This is in a two player server

I’m assuming that it has something to do with the fact that it doesn’t teleport the player.

local players = Players:GetPlayers()

for index, player in ipairs(players) do
    local character = player.Character or player.CharacterAdded:Wait()

    if index == 1 then --Just teleporting them
        character:SetPrimaryPartCFrame(CFrame.lookAt(Vector3.new(0,5,40),Vector3.new(0,0,0)))
    elseif index == 2 then
        character:SetPrimaryPartCFrame(CFrame.lookAt(Vifctor3.new(0,5,-40),Vector3.new(0,0,0)))
    end
end

does this work?

An alternative would be to just enter workspace and find player.name which == character with waits of course to ensure it exists.

Screen Recording 2021-09-04 at 5.36.58 PM.mov - Google Drive. For more context.

The characters parent is always the workspace unless you change their parent in your scripts

Try what @D0RYU said

Keep in mind he made a typo in the second elseif he wrote vifctor3 just change that and let us know

I’m not sure why the parent is not workspace, if you see the video link I posted above this, it prints out the character and its parent. However, the second character’s parent is nil which I’m assuming is why it doesn’t teleport.

So very important question how many players are you testing with

Just 2, I’m not sure how that typo got in there, there’s no typo in the code. Nevertheless it still doesn’t work. Have you seen the screen recording?

actually the original poster made the typo, I forgot to change it

Not yet lemme check it.

Also sorry @D0RYU

1 Like

I just checked the recording and it teleported you both… so uhhh what’s the problem

The first teleport is fine, I’m worried about the second teleport in the place. You can see that the first player got up to the platform fine, that’s supposed to happen. However mine didn’t. You can see that I printed the character and the character’s parent and the player that didn’t teleport was an orphan with a parent of nil for some reason.

Why don’t you try to just tp the players to the part position + a few studs on the X and Y

I will be gone for around 20 mins I might be able to make one more response but then I will be gon for any 20 mins

I’m not sure if that would affect anything, I think this mainly has to do with the character loading in.

I’ve just tried it and it sadly has not had the effect hoped for.

No, this doesn’t. Even with the typo fix the same error occurs.

Lol just figured it out, just use workspace:WaitForChild(player.Name)

1 Like