Currently I’m trying to figure out how to return from a nested function but don’t know how to, I’ve looked around the dev forum but there wasn’t anything that helped me, heres an example code:
local function a()
local function b()
return -- only returns function b
end
end
how would I make the code return function b and function a
help would be greatly appreicated!
local function a()
local function b()
-- return false when you want to return
return false
end
-- code
-- ...
local success = b()
if not success then return end
-- more code
-- ...
end
There’s no special syntax for what you want to do, because functions can be nested any number of levels deep, and each could have a different number and type of return values. So you have to write this yourself, i.e. returning a value from b() that causes a() to return also. You could also call b() multiple times from inside a(), and the code that called a() might need to know which call to b() returned. Something like:
local function a()
local function b()
if ( some test condition ) then
return true
else
return false
end
end
if b() then return "returned from first call to b"
--some more code
if b() then return "returned from second call to b"
--more code
return "returned from end of a"
end