I am trying to become accustomed to using ModuleScripts. I already know that I have to call require()
on any ModuleScript to run it. My question is how do you use specific functions you define in a ModuleScript in a Script or a LocalScript?
Documentation for ModuleScripts is available on the Developer Hub, which is where you should begin your search first before asking here. The difference between searching first is that you’d be asking questions on how to utilise or understand parts of a ModuleScript over asking how to do something pre-explained.
There’s many different ways in which you can utilise a ModuleScript to determine functions for usage. The point of a ModuleScript is that while it does run code like a regular script on require, it only runs top-level code once and every subsequent require after the first time will return the exact same references or whatever you define.
--- Example of a function-only ModuleScript
-- Module
return function()
return "ya"
end
-- Script
local funnytext = require(module)
print(funnytext()) --> ya
--- Example of a module holding things you can access
-- Module
local Module = {}
function Module.Method()
print("yes")
end
return Module
-- Script
local utilities = require(module)
utilities.Method()
A fairly common usage of ModuleScripts on Roblox is to perform object-oriented programming (OOP), wherein your modules act as classes or subclasses. This is helpful for when you want to streamline behaviour for something.
-- Module (Class)
local Class = {}
Class.__index = Class
function Class.new() -- Instantiator
return setmetatable({}, Class)
end
return Class
-- Script
local class = require(module)
local something = Class.new()
There is no “one way” to use ModuleScripts. You can use them as you wish. ModuleScripts are most renown for their use in reusable code bases, where you avoid duplicate scripts and instead use a ModuleScript to fetch code or functions or whatever.
You can even use ModuleScripts to hold data, constants and settings for stuff.
One thing to watch out for is the require statement. Remember that when you call require, the ModuleScript will run in the environment it is called from. If the ModuleScript is required from the server, it will become a server environment. If it is called from the client, it will run in a client environment. The server and client each get their own copy of the ModuleScript on require.
It’s great that you’re beginning to learn modulescripts! The ability to communicate in-and-out of modulescripts make them the perfect tool when trying to apply OOP practices.
All new modules will always start with this familiar template:
local module = {}
return module
This suggests that, once the require()
function is called on a module, a table will be returned. This table will be used in order to hold all the functions you intend on having communicated outside.
There are also different ways of inserting functions into the table – it’s all a matter of preference (afaik)
-- one way
local module = {}
function module.DoSomething()
print("Hello World!")
end
return module
-- another way
local module = {
DoSomething = function()
print("Hello World!")
end
}
return module
All the scripts (where the module is called) have to do is call the designated function through the variable in which you indexed your module from.
local mymodule = require(script.ModuleScript)
mymodule.DoSomething() -- output: Hello World!
You can return any value. Typically, developers return tables along with ModuleScripts which is why they begin like that.
return 1 -- Valid
return true -- Valid
return nil -- Valid
return workspace:WaitForChild("BasePlate", 5) -- Valid, returns BasePlate or nil
return tostring("1 yes") -- Valid
return BrickColor.new("Really red") -- Valid
return {} -- Valid
return setmetatable({}, {}) -- Valid
-- etc
One rule is that the return can’t be blank. nil does not count as a blank in this circumstance.
I know. It was specific for that particular situation, but I definitely should’ve mentioned that it wasn’t only isolated to returning tables.
Thanks!
Don’t think this was mentioned but think of a module script where its a hub which you can refer to as many times as you want. This makes your code more efficient by using the principle DRY (Don’t Repeat Yourself)
Are void methods possible with module scripts? For example in Java you’d have a class with your methods and such and have void methods inside, or does it all have to be a return method?
Your module cannot return absolutely nothing. However, you can have a “void” function in Lua.
i.e
local function test()
-- idk something that doesn't return a value
end
return test