Recursive function not returning anything

ok let me explain real quick, i have made a recursive function which doenst return anything. i have tried making a variable and then changing the variable with the function but it didnt work

local function getLowestBranch(branch)
		if branch:FindFirstChild("Rodzic") ~= nil then
			local lowerbranch = branch.Rodzic.Value
			getLowestBranch(lowerbranch)
		else
			return branch
		end
	end
1 Like

currently it has no ‘return’ function

you may need to add something like

return Table

at the end of it, once it has searched every descendant and added all of its ancestors into the table

aw man i have posted wrong function… i have edited and posted the one which doesnt work, my bad sorry lul

1 Like

aw man i think you’ve got the problem i had a while ago returning magnitude, it always comes back with ‘nil’ or ‘0’

I fixed it by just sticking it into a module script.

only change you need is to call them

local module = {}

function module.getAllBranchesAbove()
    -- the stuff
end

function module.getLowestBranch()
    -- the stuff again
end

return module

then require that in another script. if it doesnt work let me know, but its the only way i got around it when i started having this problem.

when you want to use a function within the module just do

module.getAllBranchesAbove()

hmm sounds promising let me try it out

image

can u call recursive functions in a module script?

image

1 Like

try using

module.getLowestBranch()

yes im dumb i just noticed lul

1 Like

haha! i do that all the time. its easy to forget when youre used to regular scripts.

let me know if it works :smiley:

image

nope i dont know if i did it right

1 Like

does ‘end’ need to be ‘end)’

sometimes functions need that

its working but now im having issues with player name

nevermind it is not working :frowning:

1 Like

im not sure how to fix this, perhaps try a remote function but im not sure it’ll work. sorry! :frowning:

Make it return getLowestBranch(lowerbranch) or else the initial call on the function will never return a value. Another way you could do this is by simply using a while loop:

local function getLowestBranch(branch)
    while true do
        if not branch:FindFirstChild("Rodzic") then
            return branch
        else
            branch = branch.Rodzic.Value
        end
    end
end

I see a problem here. You forgot to add the function keyword in front of the first line, so the script doesn’t perceive it as a function, which is why it’s confused about that last end

Here’s the original

local function getLowestBranch(branch)
	if branch:FindFirstChild("Rodzic") ~= nil then
		local lowerbranch = branch.Rodzic.Value
		getLowestBranch(lowerbranch)
	else
		return branch
	end
end

and here’s the solution. See if you can find what changed

local function getLowestBranch(branch)
	if branch:FindFirstChild("Rodzic") ~= nil then
		local lowerbranch = branch.Rodzic.Value
		return getLowestBranch(lowerbranch)
	else
		return branch
	end
end