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