I’m trying to get the players characters children when the character is added. However, when using both the CharacterAdded and CharacterAppearanceLoaded event given its chara parameter it results in an empty array.
The following code is in a local script, located in StarterPlayer → StarterPlayerScripts
local player = game.Players.LocalPlayer
local chracterAdded = function(chara)
print(chara:GetChildren())
end
player.CharacterAdded:Connect(chracterAdded)
The results are an empty array.
Upon looking at the API documentation closely it states, " Note that the Humanoid and its default body parts (head, torso, and limbs) will exist when this event fires"
CharacterApperanceLoaded results in the array stilling be empty. Using .ChildAdded might result in what I’m trying to achieve however i’m wondering as to why my current code isn’t working.
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
repeat wait() until #Character:GetChildren() > 4 --// just a random check you can also do Character:WaitForChild('HumanoidRootPart') or whatever instance you want
print("Character Childs", Character:GetChildren())
Unfortunately the documentation is incorrect. Roblox is still having difficulty fixing the avatar loading events ordering, so CharacterAdded actually fires before the character is even assembled at all. Additionally the character model isn’t guaranteed to have children when its created so it can’t be atomically replicated to the client.
For this issue, you are going to want to queue a replication change on the character and wait for the client to receive that change. This should roughly guarantee that the character subtree exists before it arrives to the client. You can do this in one of two ways.
One: On the server, connect to CharacterAdded. Check when the character is parented to the workspace and then fire a RemoteEvent to the client. From the client, connect to that RemoteEvent. When the RemoteEvent is fired, you’ll have the character subtree loaded.
Two: Same thing but with attributes instead. For the server, starting at 0, every time you want to try to guarantee an instance’s subtree at a certain point in time, call a function. Increase that number by one then set the number as an attribute on the instance. Let the client wait until the attribute exists on the instance it wants to check and then confirm that the global counter also changed to be the same number as or higher than the attribute on the instance.