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?
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
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
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