Unusual Behaviour with CharacterAdded

Today, when helping out a friend of mine with programming, we ran into a problem. The following code works as you would expect.

local Player = game:GetService("Players")
local Character = Player.Character or Player.CharacterAdded:Wait()

print(Character) -- Character
print(Character:GetChildren()) -- {} --> blank table

Character works fine. However, what I found interesting about this particular case, was that upon using Character:GetChildren(), a blank table is returned. The console shows as follows:
image

This is quite puzzling to me, as I personally have never encountered this problem before. The script is located in StarterPlayerScripts, and there are no other plugins or scripts that could be interfering.
We found an annoying fix. Using WaitForChild to get any instance you would expect to find in the default character, such as the Humanoid, and then using GetChildren then produced what you would normally expect- an array with all of the children inside.

Character:WaitForChild("Humanoid")
print(Character:GetChildren()) -- {"Humanoid", "HumanoidRootPart", ...) etc

I was wondering if anyone has encountered this problem before? Or if it is potentially a result of the new studio update(s)?

1 Like

I’ve done a bit of testing on this and I think the problem is, the script is running before the character loads in, so all though it’s defined there isn’t actually anything in it. If you need the character for a script, it should go into StarterCharacterScripts or you can simply bite the bullet and wait less than a second for it to all load in.

Well yeah, that much is obvious. But that has never been the case prior, and I don’t have any issues with it myself. Using the same code on my own computer, inside a script stored in the same place, I do not encounter this problem.
Waiting whenever not 100% necessary is just a waste of time and is not optimal. StarterCharacterScripts isn’t optimal either since you ideally want to keep localscript and serverscript count minimal. The above code was ran using both modules and localscripts, and both produced the same result for my friend but not me.