I've seen in some scripts it would say require(id). Is that used to place the script into Workspace? Like, sort of an encryption for it, so only the model owner could access the true script? If that's not the purpose, is there a function somewhat like that?
What is the purpose of require()? Could it be used to protect scripts?
2 answers
What's a ModuleScript
Modules
ModuleScripts are essential when trying to avoid using the same functions over and over again. Functions, tables, and just about any variable set in a ModuleScript can be used in multiple scripts without the need to re-write them all over again.
Modules scripts only run once no matter how many times called in a server, therefore, the results will never change. When called from LocalScripts, however, it runs once for each individual client that calls require.
Require
require comes into play because ModuleScripts do not automatically run when the server initiates, but rather waits until required, and then executes, returning whatever it was set to return.
Module script
Note that you don't have to return tables, but could return just about anything
local module = {
[1] = 'Scripting';
[2] = 'Helpers';
};
return module
Server or Client Script
local module = require(['path to module'])
print(module[1], module[2])
AssetIds
Also, requiring modules upload to the ROBLOX website is also possible as long as the module you require is named MainModule (not the model, the instance at time of saving). It can be load using its assetId
require(1)
Example
MainModule
local debris = game:GetService('Debris')
local function createMessage (message, time)
local screen = Instance.new('Message')
screen.Text = tostring(message)
screen.Parent = workspace
debris:AddItem(screen, time)
end
return createMessage;
Server or Client Script
Note: if the module only returns a function, you can save time by doing something like require(id)(arguments) instead of *local module = require(id); module(arguments)
require(id)('Scripting Helpers', 5)
--[[ would create a message in the workspace
with the text `ScriptingHelpers`, and will
only display for five seconds as indicated
by the arguments.
]]
require(game.Workspace.ModuleScript)
You use this to access variables inside of modules scripts for example, this is a module script in workspace
local module = {}
module.Variable = "hi"
return module
now in a normal server script
local modulescript = require(game.Workspace.Module)
print(modulescript.Variable)
hi
You can use this to store functions and a lot of stuff, In short, require gets data from a module script
This discussion is preserved as it was. New answers, comments, and votes are closed.

5 comments
So, in the MainModule module, could I put normal ROBLOX scripts? Such as, creating a message?
yeah, but more in a form of a function or table of functions. When you require the module, you could then call those functions and have them do what they are set to do.
Could you give a little example?
check the last section, "Example"
@ImageLabel Congrats on 100 reputation!