Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

What is the purpose of require()? Could it be used to protect scripts? [closed]

Asked by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

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?

Locked by theCJarmy7, LateralLace, brokenVectors, and Avigant

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
13
Answered by
ImageLabel 1541 Moderation Voter
9 years ago
Edited 5 years ago

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.
]]
0
So, in the MainModule module, could I put normal ROBLOX scripts? Such as, creating a message? Shawnyg 4330 — 9y
0
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. ImageLabel 1541 — 9y
0
Could you give a little example? Shawnyg 4330 — 9y
0
check the last section, "Example" ImageLabel 1541 — 9y
1
@ImageLabel Congrats on 100 reputation! Shawnyg 4330 — 9y
Ad
Log in to vote
4
Answered by 9 years ago
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