How do I use require()?

Hey, so I was looking into require() for making a model. I was looking on the dev forum and API, and couldn’t find what I was looking for. I know that you can use require() to send and receive things from a module script. My question was about using require(646223486)(Numbers are just an example of what I am talking about).

What does it do?

How do I use it?

What does it get?

What are the advantages to using require() to get something?

How can I use it in a script?

3 Likes

The id gets a public module script from the library.


Well, it’s mostly used for External module scripts that you probably don’t want inside your game. But can also be used for malicious purposes (such as hiding game viruses).

ModuleScripts are used to do the heavy lifting such as data stores and fully functioning systems that multiple scripts can connect to.

https://education.roblox.com/en-us/resources/coding/module-scripts/intro-to-module-scripts

https://education.roblox.com/en-us/resources/coding/module-scripts/creating-with-module-scripts

2 Likes

You can use it to import modules from outside of your game. It only works if the model is open to the public, or if you or the group the game is in own the model in yours or the group’s inventory. If I am wrong please correct me.

Could I use a module script to get a model like using,

module = {}

local insert = game:GetService("InsertService")
local assetID = 13765275862

insert:LoadAsset(assetID).Parent = workspace

return module

? I would assume not.

Yes, ModuleScripts are like Scripts and LocalScripts except they use require and allow you to use them as tables, functions, or any other values. Whatever is returned in a module script is what the scripts will see.

another example

return function(...)
   -- this makes the module script return a function instead of a table
   print(...)
end
-- in a script
local console = require(script.ModuleScript) -- the module script's location

console("hello", "this is an example of how to use modulescripts")
-- prints these strings into the output log
1 Like

Thank you, I got it to work properly.