Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is Player Get Children returning 0?

I am making a for loop to filter through the players character. It wasn't working. I figured out why. I put the players Children in a variable called d. #d returns 0. This is the code I use to get D:

local player = game.Players.LocalPlayer
local d

player.CharacterAdded:Connect(function()
	if player.Character then
		d = player.Character:GetChildren()
        print(#d)
	end
end)

However, if I try doing something like this:

print(d:WaitForChild("Head").Name)

It returns "Head" Why is #d not working?

2 comments

0

Note, the second example is if I remove the GetChildren part

1

try changing to :GetPlayers()

1 answer

:GetChildren() Gives you a list and if you print a list i dont know if you can uderstand it, do the way you can print a list is by using for loops

Example:

local player = game.Players.LocalPlayer
local d

player.CharacterAdded:Connect(function()
    if player.Character then
        local d = player.Character:GetChildren()
        for i,v in pairs(d) do --looping through the list and returns the name and the object 
		print(v) --v is the object
	end 
    end
end)

you can get an object out of a list also if you do this d[1]

2 comments

0

Thank you, but the for loop returns "Expected table, got nil" Presumably for the pairs

0

add a wait after line 5

This discussion is preserved as it was. New answers, comments, and votes are closed.