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.
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.