For Loop not looping through list

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    For testing purposes I’m trying to print out the list from a test folder. My actually intention is to get the Humanoid of all the NPCs from a folder. I have looked though other forums and videos but just didn’t work. If there is any other forums that I may have missed please let me know.

  2. What is the issue? Include screenshots / videos if possible!

Script:

local npc = script.Parent
local list = workspace.TestFolder:GetChildren()
--local tab = {"1", "2", "3"}

for i,v in pairs(list) do
	--if v:FindFirstChild("Humanoid") then
		print("Got list")
	--end
end
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried making an actually table and it would return the table but not the list, I’m not sure if this is correct but GetChildren() is sopposed to return the children of the specified instance, but in this case I’m not sure why its not returning anything.

What is located in workspace.TestFolder? There should be children parented under that folder, otherwise it won’t return anything. Another possibility is that the children aren’t loaded yet when this script is running.

2 Likes

I have 10 NPCs located in the folder. So I tried doing
local list = workspace:WaitForChild("TestFolder"):GetChildren()
but that didn’t work.

1 Like

Does it print anything if you run it via the command bar in studio?

1 Like

Yes, it does print when I run in the cammand bar. but it only prints what I wrote local list = workspace:WaitForChild(“TestFolder”):GetChildren()

If it only “prints what you wrote”, like this:

> code

Then it hasn’t really printed anything. That’s just it stating what command bar code you’re executing. In order to print something, use print(). Try putting print(list) at the end of the command bar code and executing it.

Ok it worked it printed a table with all the NPCs in it, but getting that into the for loop is what I don’t know how to fix.

Does your script now include the workspace:WaitForChild("TestFolder") part?

When I run it in a script in the for loop it prints nothing, but when I print it outside of the loop it prints an empty table.

There is a chance that the npc’s didn’t have time to load. Try adding a wait(1) at the beginning of the script.

It worked. But I hope you don’t mind but I’m trying to make an NPC that chases the other npcs in the test folder, so I have the AI script done but the part where the NPC does damage to the other NPCs in the test folder doesn’t work, that was where the problem where I wasn’t able to get the Humanoid from the for loop. When I put the loop plus the wait() you said into the AI script then it doesn’t work.