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

How do I call a function inside of itself?

I want to get every child of something, with a function, with calling the same function inside of it. But it errors saying that there's no function at all.

local GetAllChildren = function(c, t)
	for _,v in pairs(c:GetChildren()) do
		GetAllChildren(v) --The part where the function runs in itself
		table.insert(t, v)
	end
end

local Table = {}

GetAllChildren(script.Parent, Table)

print(unpack(Table))

2 comments

0

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.

0

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.

1 answer

✓ 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 --prevent unnecessary recursions
			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
		--------------------------------------------------------------
		--So that we don't try to unanchor non-parts
		if v:IsA("BasePart") then
		--------------------------------------------------------------
			v.Anchored = false
		end
		if #v:GetChildren() > 0 then
			GetAllChildren(v)
		end
	end
end

unAnchor(script.Parent)

9 comments

0

Just tried the exact code, and this weird error pops up: "Players.Player.PlayerGui.snip.test:3: bad argument #1 to 'insert' (table expected, got nil)"; I don't know why it says there's no table, when there clearly is.

0

I'm afraid I didn't get that error when I tested it. So i'm not sure why it would error for you.. try again?

0

I've tried it more than 20 times, including after reading this comment, and it still errors the same thing.

0

Okay, what is your script's Parent?

0

Here's a picture: http://prntscr.com/5ndw70

0

Okay, then what's your objective? Just to log the children? Or are you gonna end up like unanchoring them all..?

0

Yeah, let's say I want to change their properties (like unanchor)

0

Well then i'll edit in a new method(:

0

Thanks! It worked :)

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