Hello everyone.
I would like to show and explain some of the types of scripting organizational methods to newer folks joining Luau and Roblox!
What is a module? (For newer folks)
Modules are simply an instance that can be created within your game. Simply create a new instance and search for ModuleScript and boom! You have a module.
Modules are mainly used through a function called
require
, which takes a parameterrequire(path)
. E.g.require(game.ReplicatedStorage.ModuleScript)
would grab the code from inside of the instance and import it into your current script allowing you to utilize it. (The code is ran, whatever is returned is used in your current script, hence why you return the value or table)
--// ModuleScript
--// Path: game.ReplicatedStorage.MyModule
local module = {}
module.Hello = "World"
function module.World()
print("Hello")
end
return module --// We return our table.
-------------------------------------------------------------
--// Main Script
local RStorage = game:GetService("ReplicatedStorage")
local MyModule = require(RStorage.MyModule) --// The code is retrieved through a table that MyModule returns!
print(MyModule.Hello)
MyModule.World()
Modular Programming
Modular Programming simply utilizes both external and internal modules that assist with the main applications of your game/frameworks. This allows for a more preferred organizational method, which essentially splits your system into more “sub-systems”. The entire purpose of this method is for re-usability of code.
To explain this a bit more. Imagine you have all your functions and values inside your script, your script becomes a total of 500+ lines of code and is looking pretty messy. Turn your functions into modules and require them!
--// Module
return function(message)
print(message)
end
--// Script
local PrintFunction = require(path.to.module)
PrintFunction("Hello World!")
Anything can be returned through a module!
I highly recommend not using this specifically for a ton of functions. Rather use this programming style to recycle your functions. E.g. creating utilities to use in multiple scripts.
Single Script Architecture
SSA(Single Script Architecture) is simply what the name implies. Everything is contained within one script. While this method is completely fine, it can become quite tedious at larger amounts of code.
local System1 = {}
local System2 = {}
do
self = System1
self.Cookie = "Chocolate"
end
do
self = System2
self.Chocolate = "Cookie"
end
print(System1.Cookie, System2.Chocolate)
--// Returns: Chocolate Cookie
This is one of the many ways to organize SSA. While I do prefer this one there are other very simple ones like splitting your code into functions. This is another type of programming called Functional Programming. Which is simply splitting your code into functions for a more clean system. There also is another form of highly used programming called Object Orientated Programming, which I won’t be covering, but take a look at it!.
For any advanced or beginner users who are curious about the differences between the following. See below
- Procedural Orientated Programming
- Object Orientated Programming
- Functional Orientated Programming
- Modular Programming