Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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.

Ad

Answer this question