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

How Can I use a recursive function to recurse through all of a table's contents and thereon?

Say I have a table of parts in the same folder which parents more parts inside of them like a tree hierarchy. Each part has an "enabled" value which tells the function to stop recursion at that point. My function is supposed to loop through every point in this hierarchy top-down, and only stopping if the "enabled" value is false.

My attempt seems to work at first, but in the first for loop it only recurses on the first element and then it stops recursion right there and only continues on the first elements of the tree hierarchy. I can't think of any logic statements to fix this. I tried using spawn but I feel like that method is too hacky. Any tips?

function recurse(highestParentParts)
	for i,v in pairs(highestParentParts:GetChildren()) do
		if v:IsA("BasePart") and v:FindFirstChild("Enabled") and v.Enabled.Value == true then
			recurse(v) -- The top recursion never moves on to i=2, i=3, etc
		end
	end
end

1 answer

Ahh nevermind. This does work. I used a return on this function and it actually halts the entire recursion process. All in all, no returning in recursion for tree hierarchies.

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