How Do I Get The Function That Called a Function?

I’m trying to create a ModuleScript that has this kind of behavior:

local exampleModule = {}

function one(thingToReturn, bool)
	if not bool then
		exampleModule.two(thingToReturn, true)
	else
		return thingToReturn
	end
end

function exampleModule.two(thingToReturn, bool)
	if not bool then
		return one(thingToReturn, true)
	else
		return thingToReturn
	end
end

return exampleModule

While this works, how would I create a second module function that also required one that behaved in the same way?

E.g.,

function exampleModule.three(thingToReturn, bool, extraVariable)
	if not bool then
		return one(thingToReturn, true, extraVariable)
	else
		return thingToReturn..extraVariable
	end
end

one relies on re-calling exampleModule.two, but with multiple module functions, I’d need a way to find out what function to call.

I found out that it’s possible to get the name of a function, and I considered passing the name as an argument and using loadstring() to call it, but I’m not sure how to handle multiple arguments, especially if I create more module functions that have differing numbers of arguments.

The only way I can think of is to find out what function called one and re-calling it, but I can’t find any way to do that. JavaScript used to (it’s now deprecated) a function to do this, and I’m wondering if there’s any Lua equivalent?

1 Like

So you can’t actually get the name of a function because the debug library in roblox is locked. If it wasn’t though, you could actually use getfenv to call the function instead of using loadstring. Again though, that won’t work on roblox.

The best you can do is have a function pass itself as an argument to the module function.