How can I get a script that is executing a module?

Hello, I was looking if there is a way to see what script is executing a module script. You know the require() and return keywords/functions that is used in module scripts I want a module script to be able to know who is executing it. This is because I am making a custom message service and I want it to display what is the script that is executing it. Is there a way to do this? Thanks

There is no way of doing this currently. require() simply fetches what the ModuleScript returns, what you could potentially do is have a function inside your ModuleScript that receives the script in question.

Ex)

local Module = require(path)
Module.Start(script)
--Whatever code you need to run
Module.End(script)

I was considering this option, and I think I found an answer but it isnt optimal. If there is no other way I will use this but the thing is

I heard about getfenv that when its called in a module script it will show a list of variables including the scripts that executed it, the thing is it shows all the script that executed it and not the specific one that is currently executing it.

So that was why I considered asking just to make sure, but thanks anyway

Do not use get/setfenv. They’re no longer supported and disable Luau optimizations.

oh, thanks for informing.Have a good day

You can use

debug.traceback()

To know which script required the module. It is meant for debugging purposes but you could use string patterns to format it

3 Likes

I think it’d be easier and more efficient to just pass the script object to the Module and have it keep track, no overhead from string manipulation and debug.traceback()

3 Likes

sorry for the necropost, but there might be a way to do this.

if you use:

game:GetObjects(“rbxassetid://id”)[1].Parent = workspace, this should return the module into workspace

GetObjects is Hidden and Plugin Security which for a non-plugin script is useless