Findfirstchild() giving me an error when it doesnt find the child

so i made a simple function that is supposed to find a child in a branch called children, and if it finds it then get the children.

heres the function:

local function getBranchChildren(branch)
    				if branch:FindFirstChild("Children") ~= nil then
    					for i,v in pairs(branch.Children:GetChildren()) do
    						table.insert(children,v.Value)
    						getBranchChildren(v.Value)
    					end
    				end
    			end

everything is fine but when it doesnt find “Children”, instead of just not running the function it gives me an error.

image
i have tried changing it to waitforchild but then i just got this
image
any help appreciated!

1 Like

Try if not branch:FindFirstChild(“Children”) then

okay let me try that ima tell you if it works

image

ok i have most likely found the issue, value is nil after cutting down the branch above
image

or maybe not nevermind that did not do the thing

That error is not of FindFirstChild not finding the child, it’s because the variable branch is nil, make sure you are passing the correct parameters.

1 Like

the ‘branch’ object is nil, which is why you can’t use the FindFirstChild function from it, and the variable is being passed through a function argument.
To solve this, find wherever the function was called and make sure whatever is being passed through it is an existing object.

For instance, getBranchChildren() will set the ‘branch’ object as nil, because you never provided an argument with the function.

1 Like