What does require() mean and how to use it?

I need some help understanding how require() works and how to use it.

8 Likes

So say you have a module script. If you want to access the code or functions inside of it, you use the require function.

local module = require() --path to module

module.RandomFunc()

end

7 Likes

ModuleScripts? Lol Idk how to use ModuleScripts. I just thought it’s something related to Scripts or LocalScripts as those are the ones that I know how to use currently, but thanks for help!

2 Likes

Require requires modules both online and offline, retrieving the value they return for others scripts. Most ModuleScripts return a table of functions. However, they can return anything.

5 Likes

You’re welcome! Module scripts will come in handy later as you can control everything with them.

4 Likes

I just thought it’s something related to Scripts or LocalScripts, as Idk how to use ModuleScripts yet, but thanks for trying to help!

2 Likes

ModuleScripts make code look clean. They are usually used to differentiate code into different categories and use logic outside the main function.
Usually when you are trying to use/call a modulescript you will need to require it.

local Module = require(<TheModuleScriptPath>)

It’s basically a standard to start up the module script

3 Likes

DM me if you help with module scripts or anything!

3 Likes

I just thought it’s something related to Scripts or LocalScripts, as Idk how to use ModuleScripts yet, but thanks for trying to help!

3 Likes

I will if I need help with anything! Thank you:)

3 Likes

You ok? You said that like 3 times bro. Do you need a ModuleScript tutorial or something?

3 Likes

Count it as external functions. You can have many things crowded into a piece of code so you could just call some modules and make it more clear like a module for setting the player’s fov or a module for setting the players’ currency. Usually a function over 200 lines is counted as big so it would be best to minimize it’s content for making it clean and easy for understanding.

4 Likes

No, I just wanted to remind you that I thought it’s something related to Scripts or LocalScripts. Idk how to use ModuleScritps, and no I don’t need a tutorial.

3 Likes

ModuleScripts are called from GlobalScripts or LocalScripts. They are no ordinary that run by themselves like the once mentioned.
If you know how BindableEvents work you could relate it to that.

3 Likes

1- Idk how to use BindableEvents

2- Idk how to use Remote Events or functions

I’m still a beginner.

3 Likes

Here this page should help:

2 Likes

I tried reading the documentation already, but it doesn’t efficiently help because my main language isn’t English. Even I tried translating it to my main language, but I still don’t understand smoothly. That’s why I’m planning on reading it when I grow up because when I grow up, my English skills will grow up as well.

2 Likes

Well if you are making small games with a lots of scripts, you wouldn’t necessarily need module scripts. They are just optional if you are going to make games that a lot of other people will read your code. If you are working as a solo developer you can script however you’d like.

3 Likes

Require executes code in a module script and returns its value.
e.g
Module script

return "Returns test"

Normal script

local module = require(module.path)
print(module) -- Prints "Returns test"
2 Likes

Okay! My game doesn’t require many scripts. It actually does but they’re repeative, but thanks for letting me know!

1 Like