Using module functions without using local module = require(<module path here>)

This is just a small question, but would it be possible to use module functions without doing

local modulename = require(<path here>)

modulename.functionlol(...)

but


require(<path here>) --or something else whatever

functionlol(...)

you could do this ig

require(PathToModule).Function(...)

Thats not what I want, I want to just require the module in some way then just use the functions like this functionlol(...)

local functionlol = require(PathToModule).functionlol
functionlol(...)

nonono

not like that either

like


--require module here without local or any variable

functionlol(...)

kinda like script, its integrated into the script enviroments

you need to access it or functionlol won’t equal to anything, what is the point in what you are trying to do…

ok then, I know that now then :cry:

You can require a module script with 1 function like this

local function hi()
    return "hi"
end

return hi

and in another file

local hiFunc = require("hifile")

print(hiFunc())

But this is rather unpractical if you need many functions - so this could be useful if you need one big function that is used a lot, however I wouldn’t use this because this can affect performance if it needs to create a new file just for 1 function.

2 Likes
return function()
    return "hi"
end

another option would be to just return the function without a local variable

I’m pretty sure what he’s asking is if you can do something like just requiring the modules with having access to all functions without making it a variable like in python you would do:

from pathToModule import *

Am I correct @PoweredToFour ?

If he wants the function to be accessed like that the best way to approach this was how D0RYU did it.

local testFunc = require("Path").thisFunc

Lua doesn’t have an “import” function, as “require” is used for module scripts.

I am aware lua doesn’t have an “import” that was just an example

I think he don’t want to reference the variable

local abc = require(<path>)

test() -- test is a function inside abc module table

maybe you can do something like that with getenv or whatever

kinda like that but just without the module / function name before it

like that ye, but getfenv idk what the hell that even does

Yes that’s what I said that’s why i did:

from pathToModule import *

Which imports everything in the module and you won’t have to use the module name to use its functions

1 Like

Ok then yeah, I ment it like that.

Yeah it’s get fenv though not recommended.

I wonder how bad the performance hit is.

someone will test it some day… i just know

I don’t see why that isn’t good enough

why risk and use getfenv(), yea my method is more code depending on how many functions are in the module, but it is overall safer then using getfenv()

1 Like