What exactly are module scripts used for?

I haven’t found one good post or video that explains this in a way that I can understand. Most of them say they are just for storing tables, but I’m seeing a ton of models with functions and other stuff in them. For example, games like Roblox HQ use module scripts for tools, or how you automatically spawn in with module scripts in your player. So can someone explain to me a little more clearly please?

Also, what does self and metatable do, and what are they used for?

You know how its wise to use functions in coding when you’re doing the same action over and over?

Imagine ModuleScripts as functions, but other scripts can access these functions aswell, not just the script the function is defined in! You could store all your frequently used functions in a ModuleScript, then require it from any script that can see that script in the game, (if you put a ModuleScript in ServerStorage, a localscript couldnt use it)

1 Like

Thanks!
But what are metatables used for? Also I have another post asking about metatables, so I would be really happy if you helped!

The general answer is always “A script that returns exactly one value”

…but that gets really deep with other scripting knowledge. For example, if you wanted some way of controlling the color of blocks, you could connect functions to that returned ModuleScript value (assuming you’re returning a table) that would be used in that process

--ModuleScript, Color Manager
local moduleScriptReturnValue = {} --probably use better variable names based on what your script is, such as "ColorManager"/"color_manager" in this case

function moduleScriptReturnValue:ChangeColor(targetPart, targetColor) -- Assigning a function (method) to moduleScriptReturnValue
    targetPart.Color = targetColor -- Changes the passed targetPart's color to the passed targetColor
end

return moduleScriptReturnValue -- moduleScriptReturnValue is a table containing the function ChangeColor, which can be written as:
-- moduleScriptReturnValue = {ChangeColor = function}

They’re used primarily as organizational tools in games, segmenting different bits of logic into different scripts

self and metatables are both used in OOP which can be a little counter-intuitive for studio, probably don’t focus on them for now

So if I were to use this :ChangeColor() function, how would I call it? And what if I used .ChangeColor()?
^
. instead of :

Well, metatables are one of the more confusing ROBLOX Lua features, used almost exclusively in Object Oriented Programming (you’ll see this commonly abbreviated as OOP), and I wouldn’t reccomend learning it right this moment unless its relevant for one of your projects.

As far as I’m aware, they more or less allow you to add functions to tables that run when certain events happen to said table. I’m not well versed enough in OOP to teach it, so I couldn’t be of much help, sorry.

I’ll link the documentation to metatables, maybe they’ll help you.

Metatables Roblox Documentation

ColorManager = require(pathway.to.the.ModuleScript)
ColorManager:ChangeColor(pathway.to.part, Color3.new(1,1,1)) -- Changes targetted part to white

The specifics between the different functions don’t really matter if you’re not using OOP.
Just know that methods ( : ) pass self automatically with self defined as whatever table the function is being called from, constructors ( . ) don’t

just stick to using periods until you learn OOP (the difference between the two will be the first thing you learn); if you must have a general formatting rule though, use constructors when the function names start with a noun and methods for when they start with verbs

What are some good youtube tutorials I can follow that you recommend?

Main way you’ll learn is through practice (trial and error) but anything by Suphi Kaner is good

metatables: