ModuleScripts help

Hey, I’ve recently came across a few ModuleScripts, and I’m hoping if anyone could quickly brief me on how they work and how to use them. That’d be really helpful. Thanks!

Note: Sorry if this is in the wrong category or something, I’m new

1 Like

There are already a lot of topic about this. Use the search bar for what you want or just read it on the Roblox wiki.

1 Like

here I made a post on this: What is the point of using module scripts and dictionaries?

Hello!

Even thought this is not the right use of this category I can still give you an idea of what ModuleScripts are used for. :slightly_smiling_face:

Alright so the whole entire purpose behind ModuleScripts (At least the main purpose) is DRY A.K.A Do not Repeat Yourself.

I will give you some examples of this so you can better understand, let’s say that you want to make a function that would change the Color3, Transparency and CanCollide values of a specifc Part and you would repeat the same process but with a different Part.

So instead of creating 500000 functions for each Part you can just use a ModuleScript for this and pass an argument which would be that other Part everytime you want to change it like so:

ModuleScript:

-- ModuleScript inside of a Script in ServerScriptService

local module = {}

function module.ChangePartProperties(Part)
   Part.Color = Color3.fromRGB(0,0,255)
   Part.Transparency = 0.5
   Part.CanCollide = false
   print(tostring(Part).."'s Color, Transparency and CanCollide properties just changed!")
end

return module

Script:

-- Script in ServerScriptService

local PartChanger = require(script.ModuleScript)

PartChanger.ChangePartProperties(game.Workspace.Part5)
PartChanger.ChangePartProperties(game.Workspace.Part7)
PartChanger.ChangePartProperties(game.Workspace.Part10)

-- Part5, Part7 and Part10's Color and Transparency just changed and now you can collide with them!

See the difference?

We just saved MASSIVE amounts of space by doing this, instead of making a new function all over again for each Part and calling them we just call a function which is located inside of the ModuleScript and we pass the Part’s location as our argument.

Additionally I recommend you to take a look at ModuleScripts in the Roblox Developer API Reference to better understand them too, you can go to it’s page directly here: Roblox Developer API Reference - ModuleScript

Hope this helped you understand ModuleScripts better, if you have any questions do not hesitate to ask. :slightly_smiling_face:

1 Like

Essentially the way how we use classes in java and python the same way lua uses ModuleScripts.

Thanks, this really helped!

(30 chars)

1 Like

@Nerkonl has a great explanation of ModuleScripts, but where ModuleScripts come into the best use is when you want to have functions that multiple Scripts use. Things like DataStores are a great use for Module Scripts since multiple scripts in your game may want to read or write player data.

1 Like

DataStores are the next thing I am going to try learn. Thanks for the reply.

1 Like