For loop not working, nothing in output

I hate to ask this, it’s such a simple problem, yet I can’t seem to find the solution to. The for loop goes through all the children in the folder. However, it’s not looping… for some reason the script decides to skip the for loop. I’ve used the print method, yet nothing…

for i,v in pairs(game.Workspace.Boats:GetChildren()) do 
 if v == nil then
  -- nothing
 else

 end
end

I don’t think v will ever be nil if it is an instance, so I see no point in that if statement
also does the instance “Boats” have children?

1 Like

If the server is new and/or no one has boats, the folder will return empty.

if it returns empty then that means it won’t run at all
still no need for the if statement

someone correct me if I am wrong but I don’t think v can be nil using :GetChildren()

2 Likes

try printing the number of children before the loop runs and see if its detecting any children in the first place

1 Like

So you’re correct. It will only run if there are children inside the folder. Currently making a work around.

That should be correct.

Only children which are non-nil should be part of the table returned by GetChildren().

I would consider the possibility of the script execution not reaching that point. Common culprits are while true() do loops above the block.

1 Like

Yeah, I made the simple work around:

local Check = game.Workspace.Boats:FindFirstChild("Boat")
if Check == nil then
 -- there are no boats
end

Instead of making 2 different objects I would do

local boats = workspace.Boats:GetChildren()
if #boats == 0 then
  --no boats were found
end
1 Like