Is using functions inside a function a good idea?

It’s messy and disorganized, primarily. It won’t affect your performance much, Lua is pretty forgiving. It’s unnecessary and makes things a lot more difficult to read.

Also, if you’re going this route, just delete the functions altogether. Functions are for code structuring and for reusable code. If the functions are going to be in the exact place that they’re going to be called and they’re not going to be used elsewhere, there’s no point.

So with your current plan, you can revise this

local Table = {1,2,3}

--Only uses one "for loop"
local function Example()
	for i, v in pairs(Table) do
		
		local function Example1()
			--do stuff
			print(v)
		end
		
		local function Example2()
			--also do stuff
		end
		Example1()
        Example2()
	end
end

Example()

into this

local Table = {1,2,3}

--Only uses one "for loop"
local function Example()
	for i, v in pairs(Table) do
	    --do stuff
		print(v)
		--also do stuff
	end
end

Example()

It does the exact same thing but it’s a lot cleaner and it’s not recreating the function variables and calling them every loop.

4 Likes