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

How do I call a function inside of itself?

Asked by 9 years ago

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))
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. RedCombee 585 — 9y
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. Goulstem 8144 — 9y

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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)
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. Vlatkovski 320 — 9y
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? Goulstem 8144 — 9y
0
I've tried it more than 20 times, including after reading this comment, and it still errors the same thing. Vlatkovski 320 — 9y
0
Okay, what is your script's Parent? Goulstem 8144 — 9y
View all comments (5 more)
0
Here's a picture: http://prntscr.com/5ndw70 Vlatkovski 320 — 9y
0
Okay, then what's your objective? Just to log the children? Or are you gonna end up like unanchoring them all..? Goulstem 8144 — 9y
0
Yeah, let's say I want to change their properties (like unanchor) Vlatkovski 320 — 9y
0
Well then i'll edit in a new method(: Goulstem 8144 — 9y
0
Thanks! It worked :) Vlatkovski 320 — 9y
Ad

Answer this question