How To Call A ModuleScript Like require(script.Module)(localplayer, money)

Hello! I was wondering if there’s anyway to do that because It would make my scripts look wayyy more professional instead of doing,

local an = script.Module

an.Main()

Example (@wind_o CMI)

Screen Shot 2020-04-21 at 1.09.37 pm
Thanks!

1 Like

You would return a function from the module.

return print -- Module
require(module)("Hello world!") -- Requiring script

That is an example. Though what makes you think that looks more professional? lol

looks more confusing imo, what you considered “unprofessional” is more neat, and what you want defeats the purpose of modules imo

3 Likes

Ok tbh it’s more confusing lol

1 Like

When you get into bigger projects and start integrating modules more heavily in your code, you’ll actually come to prefer returning a table of functions instead of just returning a raw function.

Returning a function from a ModuleScript is good for if you only use that ModuleScript for one thing, otherwise please never actually do this in production code. It makes your code look worse, can confirm as someone who’s worked with messy code that had this in. It wasn’t good at all and it wasn’t maintainable either, especially when that function started doing a lot more than just one action.

I don’t know what “CMI” is but in this case it just looks to help you specify a certain tier of something (see? confusing) when you require the module. The returned function takes this argument and runs some kind of process against the specified argument.

1 Like

Modules are just chunks of code you can set aside and use in different scripts.

If you really only need a single function shared. Making the whole module a function is reasonable. More common is a library of functions.

module = function:

local module = function() do stuff end
return module

module as a library

local library = {}
library.funcOne = function() do stuff end
library.funcTwo = function() do stuff end
return library
2 Likes

CMI is a hotel Check-In system by @wind_o just to let you know :smiley:

1 Like

Here’s a quick explanation. When you call the require function on a ModuleScript instance, that ModuleScript is run. If it returns something, then that’s what you get from it.

-- MyModule.lua
return "Hi"
-- script
local returningValue = require(script.MyModule)
print(returningValue) -- Output: Hi

Now let’s look at this statement:

require(862849844)('Enterprise')

We can spread this out, to this:

local returningValue = require(862849844)
returningValue('Enterprise')

It’s quite clear that the module is returning a function, which is run with the parameter "Enterprise".

Now that we know how to replicate something like this, let’s try it!

-- MyModule.lua

local returningFunction = function(text)
    print(text)
end

return returningFunction
local returningFunction = require(script.MyModule)
returningFunction('Enterprise') -- Output: Enterprise

Or, if you want to keep the same format as CheckMeIn, you could just do:

require(script.MyModule)('Enterprise') -- Output: Enterprise

Note: CheckMeIn has a number in the require statement because it’s requiring the module via it’s asset ID, which can be done if the module is published to ROBLOX and is named MainModule.

2 Likes

FYI: You can also use a metatable with the __call method assigned, then that table can contain more function members as well.

--// ModuleScript
local Module = {};
local Metatable = {};

function Metatable:__call(String)
    print(String);
end

function Module.Function()
    print("Function called!");
end

return setmetatable(Module, Metatable); 
--// Script
local Module = require(script.ModuleScript);
Module("Table was called!"); --// Table was called!
Module.Function() --// Function was called!
1 Like