5 = 0 (apparently)

I have a folder named “Patrol” with 5 parts in it. Whenever I try to get the number of children, it returns 0.

    local patrolPoints = game.Workspace:WaitForChild("CurrentMap"):WaitForChild("Patrol")
	local waypoints = patrolPoints:GetChildren()
	if waypoints then	
		warn(#waypoints)
    end

What is the parent of the folder and are you using a local script or a normal script?

Edit: Nevermind I see the parent now but is it local or normal?

Parent is the “CurrentMap” inside the workspace, its a server script.

You need to loop through it first.

for i,v in pairs() do

I don’t think this is necessary to get the number of children

1 Like

This used to work it just stopped for some reason.

Are you positive that you are looking at the right folder with 5 parts in it?

Ah, just realized there was a :GetChildren() there, my bad.

Using #tablename will return the length of the table (it should)

Yes, I didn’t see the GetChildren at first.

1 Like

maybe try doing

    local patrolPoints = game.Workspace:WaitForChild("CurrentMap"):WaitForChild("Patrol")
	local waypoints = patrolPoints:GetChildren()
	if waypoints then	
		warn(#patrolPoints:GetChildren())
    end

warn(#patrolPoints:GetChildren()) is not necessary.

but why isn’t it working for them… lol

Hard to say without looking at their explorer. It’s possible that none of the children are being loaded in at the time of using :GetChildren. It’s also possible that the first child found and placed in the table is being read as nil. If that is the case, then trying to find the table size using #patrolPoints:GetChildren() would yield 0.

1 Like

Oh that makes sense, but how would you go about fixing this? Because you can’t just wait for children

Technically you can…WaitForChild

Are the children the same every time the script runs?

WaitForChild waits until a child with a given name is found. It will not wait until all children of that child are loaded in.

1 Like

If you’re looking through the folder intended, it sounds like you ran getchildren before any of the children even existed, you can debug this easily like so,

local patrolPoints = game.Workspace:WaitForChild("CurrentMap"):WaitForChild("Patrol")
local waypoints = patrolPoints:GetChildren()
if waypoints then	
	warn(#waypoints) -- This is your current first attempt
	warn(patrolPoints:GetFullName()) -- Does this print the folder you want to look through?
	for i = 1,10 do
		wait(.5)
		warn(i,#patrolPoints:GetChildren()) -- check if any of this prints the right amount
	end
end

If any of those 10 loop prints 5, then you know it’s the issue of them not loading in yet :+1:

1 Like

ContentProvider:PreloadAsync() could be used to ensure that all instances have been loaded in before running code.