So normally I do stuff like
things = require(directory.to.ModuleScript)
things.customFunction(123)
But what if I wanna do something like
require(directory.to.ModuleScript)
customFunction(123)
or maybe
require(directory.to.ModuleScript)()
customFunction(123)
Is there a way to do this? I’ve messed around with getfenv
but can’t seem to get it to work.
Was there some sort of update that changed getfenv behavior? The wiki seems to have inaccurate information. In the code snippet for “Getting the Environment Based on Stack,” env.password
prints nil, and not the variable from one level up as expected.
My friend @OutOfDinos helped me out!
for those interested, here’s how you would do it
-- module script
return function()
local env = getfenv(2)
env.fun = function() end
end
-- local script
require(module)()
print(fun)
apparently he doesn’t approve of this, but i still think it’s epic so ima use it anyway LOL
1 Like
Did he ever explain why? If he didn’t I can: it’s because get/setfenv disable optimisations by Luau. In this day of programming on Roblox you should never be using get/setfenv to add custom functions to the environment, just use a ModuleScript and import your functions directly.
Not worth disabling code optimisations for the sake of making the code elegant when only you (and if applicable, collaborators) will read. I realise this is not answering the question but it’s a little bit of a warning in using these functions for you and future readers - I don’t ever recommend doing it.
7 Likes
Oh that’s interesting, I’ll keep that in mind.
For those wondering where he’s quoting this, I’m pretty sure it’s from here: Performance - Luau
kinda lame but ¯\_(ツ)_/¯
I guess performance trumps pretty code