Output is printing that character's parent is nil

RunService.RenderStepped:Connect(function()
print(character.Parent)

I think it’s pretty self-explanatory at this point. The output is printing “nil” when the character.Parent is clearly in workspace?
Keep in mind there is no “custom characters” involved.

Been going back and forth trying everything and still can’t find any solutions.

Anyone?

What have you defined ‘character’ as?

local character = Player.Character or Player.CharacterAdded:wait()
2 Likes

Try printing character:GetFullName() and give us the result.

1 Like

R15 characters actually “spawn” twice in quick succession. This is a technical detail that is not well documented. It is possible that “character” is currently referring to the first spawn of your character, that has since been replaced by your actual character (the second spawn).

To fix this you should run your code on every CharacterAdded invoke, not just on the first one.

5 Likes

I’ve seen a lot of code rely on the original behavior for things like custom cameras, movement, etc. Wouldn’t it be a good idea to document something as potentially game-breaking as this, or at least a little heads-up somewhere?

2 Likes

I wouldn’t describe it that way because this has been the way it is for ages (and clearly isn’t breaking games) and you really should be running code on every CharacterAdded invoke, not just on the first one.

You can use #platform-feedback:documentation-requests for requests for information to be added to the Developer Hub, please search beforehand that this hasn’t been requested before by using the search functionality in the top right of the forum.

3 Likes

Prints my name just fine.
image

3 Likes

I already did something similar to this. Basically, I have a function that fires whenever the character respawns.

local function Spawned()
    wait(1)
    repeat
       wait()
    until character.Parent ~= nil
    character = Player.Character
    Humanoid = character:WaitForChild("Humanoid")
end
Player.CharacterAdded:Connect(Spawned)
1 Like

GetFullName is supposed to return the full path to the object. For it to be returning just the name means it doesn’t have a parent. Because of this, I’m assuming your issue is as @buildthomas stated.

2 Likes

You need to use CharacterAppearanceLoaded, rather than CharacterAdded, for R15 characters.

The reasoning for this, is like @buildthomas said, the character “spawns” twice before loading. It “spawns” twice if a player has a bundle equipped, because rather than having meshes parented to the character for bundles like R6, R15 loads in a completely new character once the bundle loads in.

3 Likes

It’s a race condition, it’s going to work or not work depending on where you run the code.

This is way too complex and could actually cause an infinite loop that never stops for the first spawn of R15 characters, since there’s a chance you will never catch character.Parent being set (OP has this issue, Parent is continuously nil for the first spawn because it has been parented out). Bad idea.

You should just do this:

local function characterAdded(character)
   local humanoid = character:WaitForChild("Humanoid")
   -- do stuff with character and humanoid
end

Player.CharacterAdded:Connect(characterAdded)
if Player.Character then
   spawn(function() characterAdded(Player.Character) end)
end

If you want to be extra sure that you are not leaking threads, add a timeout on the WaitForChild and return instantly if humanoid is not set.

8 Likes

Thank you!

1 Like