✓ Accepted answer
Maybe the reason your script isn't working is because you tied a variable to a function, rather than making a standard function value. Also, you have to call the function inside itself AFTER you define the code inside the function.
Try this?
function GetAllChildren(model, tab)
for i,v in pairs(model:GetChildren()) do
table.insert(tab,v)
if #v:GetChildren() > 0 then
GetAllChildren(v)
end
end
end
local Table = {}
GetAllChildren(script.Parent, Table)
print(unpack(Table))
-EDIT-
Wanting to unanchor models? Use the same method as above but instead of inserting the current instance into a table, set it's anchorage to false(:
function unAchor(model)
for i,v in pairs(model:GetChildren()) do
if v:IsA("BasePart") then
v.Anchored = false
end
if #v:GetChildren() > 0 then
GetAllChildren(v)
end
end
end
unAnchor(script.Parent)
2 comments
You can call a function inside another function, but you can't call the function itself inside itself. That doesn't even make sense because if you call the function in itself that would essentially loop forever and never get anywhere.
That is completely inaccurate, RedCombee. You can definitely call a function inside itself, think about getting all the decendants of a model.. or say you're re-picking a math.random value until it doesn't equal something else. Function recursion is completely compatible with Lua.