How to return a function thats inside a seperate function?

I’m just trying to do some stuff in lua, but I quite honestly dont know how. The issue is that I’m trying to return a function inside a function but it doesnt work.
Here’s a simple version what I’m talking about

function hi()
	for i, v in pairs(table) do
		if true then 
			function bye()
				return
			end
		end
	end
end

hi()

The thing is though when I do return, it only returns the bye() function and not the hi() function. Is there any way to return the hi() function inside of the bye() function?

You should not define functions within functions.

A good substitute for what you are trying to achieve is ModuleScripts.

i’m not really sure what you’re looking for

function Chat(Responses: {string})
  for _, Response in Responses do
    if Response:lower() ~= "hi" then continue end
    return function()
      print("Bye!")
    end
  end
end

local Bye_Function = Chat({"Hi"})
Bye_Function()

The hi function is supposed to be a module script function.

I’m trying to return a function outside of itself.

You can’t directly make a function return using code in a function that is called inside it. But here’s how you can do something similar.

local function shouldMainFunctionContinueRunning(valueInTable)
	if --[[some condition using valueInTable]] then
		return true
	end
	return false
end

local function mainFunction()
	for i, v in pairs(table) do
		if not shouldMainFunctionContinue(v) then
			return
		end
	end
	--[[
	Code that does other things.
	This code won't run if shouldMainFunctionContinue
	returned false for any table value.
	]]
end

mainFunction()

Also, in the code you posted, you don’t even call the function bye so it doesn’t return either.

Ah well thats a shame. Thanks for the help!

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