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

How to use require properly?

Asked by 8 years ago

So I have a script that requires a module from roblox:

require(3858375)  --Not actual id

And I am wondering about the usage of some things. For example, I was looking at another person's require and it had this:

require(475858285) (script)

What is the (script) used for? The other question is: How would I call a function from the required module. Would it be:

require(3858475:Initialize())

Any help would greatly be appreciated.

1 answer

Log in to vote
1
Answered by
Link150 1355 Badge of Merit Moderation Voter
8 years ago
Edited 8 years ago

There are two ways of require()ing a Module script.

The first one is by passing the module script as argument: require(game.ServerScriptService.MyModuleScript)

The second is by passing it the assetID of a Module script that has been published to Roblox as argument: require(3858375)

A module script must return at least one value. The type of this value and its usage is up to the user, however. require returns that value, which can then be used by the require()ing script. Usually you would return a table, formatted as a dictionnary, which contains functions and optionally variables.

For example:

-- game.ServerScriptService.MyModule

local module = {}


module.a = 20

module.foo = function()
    print("Hello from 'bar'!")
end

-- Another, more intuitive, way to write module functions.
-- This is just syntactic sugar:
function module.bar()
    print("Hello from 'bar '!")
end


return module

And then from a normal script:

-- Normal script

local myMod = require(game.ServerScriptService.MyModule)


myMod.foo() -- This would print "Hello from 'foo'!".
myMod.bar() -- This would print "Hello from 'bar'!".

print(myMod.a) -- This would print "20".

Since you are able to return anything you want from a module script, you could also return a function, which is what allows for crazy code such as:

-- I directly call the function returned by requiring the module script
-- with ID "3858375".
require(3858375)()

So the require(475858285)(script) part you mentioned is really just sending a reference to the current script as argument to the function returned by require.

Hope that helps. If it did, please make sure to mark my answer as accepted and optionally upvote, it helps us both. :)

If you have any further questions, feel free to ask.

0
Yes and no, I understand about the (script) part now, but how would I call a specific function from the modular script if I was requiring it from online? The script has 2 functions and I only want to call one of them. UltChowsk 85 — 8y
0
Well, I showed how to call a module function from the require()'ing script in my example. It depends on the module script, really. But I'd assume the function returned by your module script, when called, returns a table of functions. Try this, for example: "local myMod = require(475858285)(script) myMod.func()". Link150 1355 — 8y
Ad

Answer this question