Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Module Scripts question?

I am able to make module scripts work in the way that I want them to, but what is the table for?

By that I mean:

local module = {}

And why does nothing go directly inside of it?

1 answer

The purpose of modules

Modules are a way to utilize your code, having a main script (any script that's requiring the module), use the code inside the module (or you could just make it so the code inside your module runs immediately upon requiring it).


"What does all this mean?"

Well you see, module scripts act much like ordinary functions. You put code inside of it, and it won't execute until it's called (or in the case of a module, it won't run till it's required). Now while we can use modules to just execute random chunks of code whenever we want, this isn't very practical (at least, not most of the time). Could you imagine having 10 modules all executing different sections of code just to accomplish the same task? It'd be pretty hard to manage.


"So, how are they used?"

Most of the time, modules are used in a way that lets you create your own library of code. You can then use this library to hold functions to help interact with your main program, settings to keep an organized reference to what you want to change in your program, or pretty much anything else that makes good use of separating the interface from the program.


"So what's the "local module = {}" about?"

This brings us to some limitations of using module scripts. You see, like functions, modules can return values to whatever required it. However, unlike functions, modules must return a value at the end of the script, otherwise it will break, giving you this error:

12:27:18.251 - Module code did not return exactly one value

12:27:18.421 - Requested module experienced an error while loading

It's also important to note that modules can only return one value - no more, no less. This is why you'll often see modules return a table, since you can store multiple values in a table - making it your own little library to work from.

Questions?

I'd go more into detail about this, but I've answered a question about modules before that you may find useful here: https://scriptinghelpers.org/questions/28541/what-are-module-scripts-for#32000

If you have any questions, just met me know.

This discussion is preserved as it was. New answers, comments, and votes are closed.