ModuleScripts General queries?

I want to try and move to using module scripts to run my games, instead of one large core game script with everything in it. Looking around at other forum posts about module scripts and how to use them, etc. I’ve also looked through youtube videos, the wiki and models that use module scripts. One thing I’ve seen quite a lot but don’t know what it does are these few words:

:Init() - I’m guessing this might be short for Initialize(), which I’ve seen used as well. No clue what it’s initializing

self: - not entirely sure what this does

Any answers or tips would be greatly appreciated :smiley:

5 Likes

Good job making the effort to learn, modules are fantastic :slight_smile:

‘:init()’ functions are indeed short for initialise, and are user declared (not a built-in functionality of modules). They are used to kick off events in a module, and are generally used for two reasons (that I know of):

  1. To allow the module to load fully before any game code is run/events are connected
  2. To allow for modules that depend on each other.
    Normally, if you have two modules that require each other, you will create a circular reference and your code will error (Module 1 requires module 2 which requires module 1 which requires module 2 which…). To get around this, you can have your first module load in the second module in it’s initialisation function. This means your ‘master script’ will call module1’s initialisation function, loading the second module. This second module will then load in the first one without creating any problems (as it won’t call the initialisation function of the first module)
    Ideally you want to avoid any circular dependencies in your code, but sometimes they may be necessary.

Initialisation functions can be named anything - module.start, module.run, or whatever sounds best to you. In practice this would look something like:

---------------------------- Module code

local module = {}
module.Players = {}

function module.init()
	game.Players.PlayerAdded:Connect(function(newPlayer)
		table.insert(module.Players, newPlayer)
	end
end

return module

---------------------------- Main script code

local playerModule = require(script.PlayerModule)
playerModule.init()

The ‘self’ parameter is to do with Object Oriented Programming, which you can read all about here and here

30 Likes

You can do whatever you want with a module - there are no preset functions. Basically, a module has to return something; usually a table.

Initit is usually used to setup the module. You can add this function pretty easily

local GoGoGadgetModule = {}
GoGoGadgetModule.ModulesAreCool = true 

function GoGoGadgetModule:Init(...)
print(self.ModulesAreCool)
end

return GoGoGadgetModule 

This would print true when Moudle:Init() is called

As you can see, self just gives the table that the function is apart of. This is only available when using the : operator however.

1 Like

Initialisation functions can also be used to pass values into the ModuleScript, as there is no behaviour for that currently.

-- Module
local module = {}
local settings

function module.init(newSettings)
    settings = newSettings
end

return module

-- Main script
local settings = {}
module.init(settings)
4 Likes

@ForyxeV @DarkContinuum @EmeraldSlash

Thank you for the help and information :smiley: I look forward to being able to use ModuleScripts more :slight_smile:

3 Likes

Wow thanks i didn’t know about this. Once I created a circular dependency and had to do a lot of reorganisation to get around it. But this will help me a lot.

1 Like

ModuleScripts are generally a charm to learn. There’s a ton of tutorials and inquiries in and around the Wiki related to them, as well as OOP. I’ll re-iterate the answers because I feel like responding, despite already having being answered.

:Init() is a function that’s usually included by scripters in their modules so that they can pass values to the ModuleScript. Normally, the scripts contain top-level data and references that the module uses later when it’s being operated.

self is related to OOP, but its usage isn’t only limited to modules. “Self” is an argument used when you want to pass the table automatically as the first argument to a function.

local yeet = {ok = "ok"}
yeet.hi = function(self) print(self.ok) end

yeet:hi() --> ok
yeet.hi(yeet) --> ok
yeet.hi() --> nope, no "self" passed

Self is really just another variable. When you run a function from any table like Table:Function(), the : part tells the function to pass the table as the first argument. When you run it like Table.Function(), it accesses the table and calls the function as normal. You should really only half “self” if you intend to call functions with :, otherwise standard method works just fine.

8 Likes