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

(Solved)How do I fix the recursive function?

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)

4 comments

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

0

Local functions can be called recursively.

0

Please accept Phlegethon's answer

0

thanks

1 answer

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!

1 comment

0

Thanks.

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