You can only return one value. That’s just how it works. Think of a ModuleScript as a function. It only runs when you require it, it can (and has to) return a value. And it runs all the code inside of it when it is called. (require)
The __call metamethod would work well for what you are trying to achieve.
local module = setmetatable(
{--the module itself
--whatever your module contains
},
{--the metatable
__call = function(self,...)
--self is the module, ... are the parsed arguments
end
})
return module
The thing to consider is that if you call it as a method (module:Part()), the first argument will be “Part”.
That works, but before we go so deep into metatables we probably would need to ask “why?” does he want to do this specific thing.
Is he trying to solve a problem?
Just learning?
or some kind of programming aesthetic choice?
Metatables are nevertheless a good thing to know even for beginners. They are confusing for people who got used to other languages, but are actually really simple to understand. They can also solve pretty much any issue with little work.
They are meant to stuff all the complicated code behind and abstract class. It is easier to use something without actually understanding it. The overhead is pretty much the only issue I see with them, though it is actually really small and only makes a difference when often accessing the object.
@mobyboyy They are literally just tables with functions or values that get called/returned in specific cases. They are fairly easy to understand.
@StrategicPlayZ As I said above, metatables are just tables that contain values that are meant to replace a default function of tables (and userdatas, but you don’t need those in Roblox). You can read the “metatable” article on the DevHub, but I’m going to make a basic run down of what each of them does.
There are multiple metamethods that can be placed into a metatable and all of them get “activated” in certain conditions.
__index: This will fire every time you try to access the table.
If you set a function to it, it will fire with two arguments: the original table and the index of the value you tried to get.
If a table is set to it, a value from that table will be returned instead.
__newindex: This one gets called when you set a new value to your table. Good for checking if a valid value is added. The arguments are: table, index, value.
__call: This allows you to use a table as a function. If you apply this, calling a table like tab() will no longer error as it reroutes your arguments to the set function. Arguments: table, method name (optional), other arguments.
There are also math and logic metamethods which allow you to add, subtract or check if the two tables are equal. Neat for implementing custom classes.
There are also other metamethods, but they aren’t as important.