Hello devforum, I’m not new to scripting, I don’t need help with how to use modules, rather how to make them useful and achieve Single Script Architecture.
I’ve tried many ways of doing this, they all ended up being useless, messy, and creating a lot of delay with events. So, if possible, can anyone explain how Single Script Architecture works and how it can be learned. (I may be able to learn it from an explanation of how it works.)
The only way to use a module is to read its documentation, which may include comments in the code itself, function signatures (the names of parameters can be a hint), and any example use cases the author has given. I’m not familiar with Single Script Architecture, it may be referring to DRY
First of all, I don’t recommend this system. Although I’ve never heard of this system before from what you’ve said above I can see that this system isn’t the best one to use in my opinion.
Here’s why:
You have a single script for your entire game - two if you count client and server, this makes it difficult to add new features and work in pre-existing code because you have to scroll through your entire script to find a single function/line of code that you need to change.
Variable limits; you are only allowed 200 local variables per script, this makes it again, harder to code and add new things to your game.
While I’m not suggesting that you have a single script for a function I believe that you can better improve this system by having multiple scripts for multiple things; for example you might have a class module for doors that controls all doors in your game.
hes talking about using a single localscript and single serverscript to require modulescripts, not every single line of code being in one script.
and in my experience this is the architecture i most likely use, especially pairing it with oop, it makes threads more manageable and errors way easier to debug (partly because scripts being cloned doesnt bring you to their source)
edit: Arsenal actually does something like this, their scripts are over 20k lines long
The limit is 200 items on the stack, effectively 200 variables per function, not per script. Anyway I don’t think theres any especially good reason to try and have everything in one script.
This is just going to rapidly devolve into a bikeshedding thread but fwiw there are plenty of reasons to have more than one script / local script, particularly if multiple people are working on a project. The fewest files I ever have even on very basic projects outside of Roblox is around three. Separate files/scripts are part of the organizational structure and you won’t find any large projects either on or off Roblox that try and pack everything into one. Doing so is just an obsessive impulse to reduce the number of “important lines”, but you can’t reduce the complexity of the project this way.
Single Script Architecture is basically where you have two scripts. One script is for the server, and one script is for the client, Hence the name. However, the only thing that really happens in these scripts is a loop that requires all module scripts to “boot them up”. In these module scripts, the modules require each other to create the game. This method of organizing your game feels somewhat neater, and utilizes many opportunities. It also allows you to have a greater sense of what loads first. Many top games use it and while I do recommend it myself, It is quite difficult to grasp if you’re a beginner.
Here is an example:
--//Main server script:
local ServerScriptStorage = game:GetService("ServerScriptService")
for _,Object in ServerScriptStorage:GetChildren() do
if Object:IsA("ModuleScript") then
require(Object)
end
end
--// A random module in ServerScriptStorage
local Module = {}
function Module:GetRandomNumber()
return math.random(1,100)
end
return Module
--// another module in ServerScriptStorage
local Module = {}
function Module.AddNumberToRandomNumber(Number)
local RandomNumberModule = require(game:GetService("ServerScriptStorage").RandomNumberModule)
local Answer = RandomNumberModule:GetRandomNumber() + Number
return Answer
end
--Code outside any function inside a module runs when required. (can run multiple times if required multiple times)
local RandomNumberPlus5 = Module.AddNumberToRandomNumber(5)
print(RandomNumberPlus5)
return Module
Of course there is no need for what these functions do in an actual game, but you can change them accordingly to your games needs
I think he meant for every system. For example, trying to set up a framework structure for abilities then you can achieve that with single-script architecture and etc.