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?
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
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.
Note that you don't have to return tables, but could return just about anything
local module = { [1] = 'Scripting'; [2] = 'Helpers'; }; return module
local module = require(['path to module']) print(module[1], module[2])
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)
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;
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
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?