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

(Solved)How do I fix the recursive function?

Asked by
brok4d 77
6 years ago
Edited 6 years ago

Hello, I'm doing a recursive function, on some models and inside there are other models and part, well then it throws me an error of stack overflow, and the script stops working, what can I do to fix it ?, I'm new and it's not that know a lot, thanks.

function ChildEdi()
    for i, child in pairs(model:GetChildren()) do
        if child:IsA("BasePart") then
            child.Touched:Connect(onModelTouched)
        elseif child:IsA("Model") then
            ChildEdi(child)
        end
    end 
end

ChildEdi(model)

2
The stack overflow is because you are calling the function over and over again because you never go "deeper." There will always be a child that is a model becuase you are just calling the same function over again with the same model User#21908 42 — 6y
0
Local functions can be called recursively. User#19524 175 — 6y
0
Please accept Phlegethon's answer User#19524 175 — 6y
0
thanks brok4d 77 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

The problem is that you are putting an argument into the function where no parameter exists. Simply add the parameter:

function ChildEdi(model)
    for i, child in pairs(model:GetChildren()) do
        if child:IsA("BasePart") then
            child.Touched:Connect(onModelTouched)
        elseif child:IsA("Model") then
            ChildEdi(child)
        end
    end 
end

ChildEdi(model)

Hope this helps and have a great day scripting!

0
Thanks. brok4d 77 — 6y
Ad

Answer this question