What are module scripts/how do we use them?

Hello,

So this is kinda embarrassing as I have been coding on Roblox for about a year now but I have never used module scripts but I would like to look into using them more. I am just wondering if anyone could tell me about them. I read the doc on them ( ModuleScript (roblox.com)) but I don’t understand it a ton.

From reading the docs on it it seems to be like what I do in JavaScript where we can create modules (where we can use functions in any scripts if we require them and return stuff). That is what I think they are kinda like I just want someone to tell me more about them.

1 Like

Module scripts are pretty much ways of storing functions and data than can be accessed from other scripts. For example, you may have a module that manages the users money, so instead of manually doing all the checks and functions to remove money in every different script, you can instead just do Module:RemoveMoney(plr, amount). ModuleScripts are pretty easy to use, you just have to return a table with all the functions and data.

local module = {}
module.Value = "asdfjdfs" 

function module:Test()

end

return module

When you require() the module, you can do module:Test() and get module.Value.

5 Likes

Ok so I kinda understand that but not sure about the code you wrote.

I just don’t really understand how it works? Should the return module not be in the function?

1 Like

Not sure what you mean by that. In a modulescript, at the end you must return a value, in most cases it being a table. In my code example, you see me define the module table, and set a value inside of it. You also see me setting a function inside of it. At the end, I return that table.

3 Likes

Do some research before posting.

1 Like

So what is the point of that? (the value thing)

What is an example of when you would use this?

1 Like

As I stated in the post I already read that. I just don’t totally understand it so would like someone to go through it.

(next time read the post plz)

4 Likes

Please don’t be rude. @jufdnhf has already answered the issues I had.

6 Likes

Your issue is even listed in the docs, you’re the one posting about something that’s already stated.

Simple ModuleScript Example
The code sample starts by creating a local variable holding an empty table. It then fills the table with two placeholder functions, and then finally returns the table. Using this code in a ModuleScript would make the my_functions table (and thus any functions, tables or other values within it) available to any Script, LocalScript or other ModuleScript.

-- Tables are store multiple values in one variable
    local my_functions = {}
    
    -- Add a few functions to the table
    function my_functions.foo()
    	print("Foo!")
    end
    
    function functions.bar()
    	print("Bar!")
    end
    
    -- ModuleScripts must return exactly one value
    return my_functions
1 Like

The reason behind this is that it allows you to index it as Module.Value in other scripts that require() the module.

1 Like

Ah I see that makes sense. I thought it was just functions that is why I was not sure what the point of it was.

2 Likes

It’s basically just a regular table, just like any other table.
You can understand more with that in mind: Tables | Roblox Creator Documentation

1 Like

He clearly said that he had read the doc and didn’t understand it much. No need to be rude, I had already helped him with his problem and you just made a completely unnecessary reply. You don’t get “brownie points” for doing that.

23 Likes