How to get the script that called a function (modules)?

Hi! I’m making quite a simple system here;

local Contexts = {
    ['Information'] = '[CheckMate] [Information] [%s] %s',
    ['Warning'] = '[CheckMate] [Warning] [%s] %s',
    ['Error'] = '[CheckMate] [Error] [%s] %s'
}

return function(Context: string, Content: string)
    if table.find(Contexts, Context) then
        warn(string.format(Contexts[Context], scriptThatCalledFunction.Name, Content))
    end
end

It should seem pretty straight forward what I’m trying to achieve here; however I’m basically making a custom printing system, in a client/server script I would be able to do local warn = require(pathToMyPrintingModule), however, I’m trying to detect inside my module what script has triggered it; would this be possible?

1 Like

I guess you could send the script name as an argument for the function

1 Like

I could, however I was wondering if there was a way to automate this process (without adding it as a parameter).

1 Like

as far as I know, I don’t know how you could do that

someone can correct me if I am wrong about this

1 Like

Alrighty, I suppose I’ll just add it as an argument if no one else responds.

Actually there is another way you can get the script object without a parameter.

We can use getfenv() like the following:

getfenv(2).script   -- This will return the script object

getfenv() basically just gets the current environment of of the caller you can read more about it here:

just scroll down to the getfenv section.

4 Likes

isn’t getfenv() unsafe compared to a parameter?

I dont think it is unsafe to use.Yes it can be used maliciously but in this case it is fine as we are just using it to get the current script.

1 Like

sorry I don’t use setfenv() and getfenv() much so when it comes to how safe they are I am unsure

thanks for telling me

getfenv is the only solution for this problem. However it will cancel all luaua optimizations! and it’s unsafe, deprecated and usually the scripts the use it are considered malicious.

So I don’t think it worth it especially for a public ready product

2 Likes

Oh I was not aware it was deprecated so yeah using perameters is probably better.

1 Like

oh yea isn’t that because of debug | Roblox Creator Documentation?

1 Like

Exactly. Also it’s no longer supported by Luaua.
So whenever the engine detect getfenv, setfenv. it will cancel the optimizations done to make luaua as fast as we see it today

So wait how good would the following be to use:

debug.info(2, "s")

because this returns the same as GetFullName() of the script.

4 Likes

Yess. as long as you’re not using getfenv and setfenv, it’s alright!

1 Like
function getScriptSource(scriptOrder)
	local traceback = debug.traceback()
	local sources = {}
	for line in traceback:gmatch("[^\n]+") do
		local word = line:match("([^:]+):")
		if word then
			local lastWord = word:match("%w+$")
			if table.find(sources, lastWord) == nil then
				table.insert(sources, lastWord)
			end
		end
	end
	return sources[scriptOrder]
end
print(getScriptSource(2))

I have tested it and it should work. Essentially how it works is that it gets the traceback string, then it extracts the caller scripts/modules names and sorts them in a table, based of the number you pass in the function it will retrieve the script from the order. So if you pass 2, then it will get the name of the script that called that modulescript.

This method avoids the need of having to pass an argument of what script is requiring the module

3 Likes

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