How to get script object from module script?

Hi,
I have problem, that i am moving code to module script and script variable is shortcut for the modal script, so almost everything isnt working, so how can i get shortcut on the script, i am executing it from?
Edit:
So if i have some script

local m = require(game.ServerScriptService.modul)
m:returnscript()

and modul

local m
function m:returnscript()
...........
end
return m

how can i make, that the function returnscript will give me the first script and not the module?

1 Like

I don’t really understand. Please explain more.

But before I answer do you know how to use module scripts?

I would pass the script object into the module script function you’re calling as an argument.

1 Like

Yes, and i edited it to give some example.

1 Like

I got this idea, but isnt there some option without it? Bec i am not sure, that i will remember all scripts, from where i am calling it.

Just off the top of my head, I believe the only other way this could be achieved is by jumping to a higher function environment scope using getfenv(). You can do the following to retrieve the script object calling the function:

local m
function m:returnscript()
    -- This is function environment 0, relative to the getfenv call --
    return getfenv(2).script
end
-- This is function environment 1, relative to the getfenv call --
return m

...

-- Your non-module script: --
-- This is function environment 2, relative to the getfenv call shown in the module script code --
local ms = require(game.ServerScriptService.modul)
ms:returnscript() -- Returns the script object running this code

Remember that “script” is a local variable, just like “ms” and “m”. “script” is just a local variable that is localized to the scope of the entire script implicitly.

1 Like