Help with recursion

So I was trying to assign a variable to a recursive function but for some reason the variable is nil.
Heres the code:

local function right(player, counter)
	if not counter then local counter = 0 end
	local yes = check(player)
	print(yes)
	if yes == true then
		counter += 1
		right(player,  counter)
	else
		print(counter) -- prints 1
		return counter
	end
end

local counter = 0
local rightcounter = right(player, counter)
print(rightcounter) -- prints nil

Any help would be greatly appreciated!

if yes == true then
		counter += 1
		right(player,  counter)
--------MISSING A RETURN, THEREFORE IT RETURNS NIL---------
	else
		print(counter) -- prints 1
		return counter
	end

I put where you are missing a return. Even if you keep running the function, it will return nil.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.