This tutorial will help you structure your projects in Roblox Studio to improve readability, maintainability, and scalability.
Keep in mind that I am by no means saying that this structure is perfect. But it is the most readable of all that I have used.
1. Code Organization
The key to success - modularity. Break your script into modules and one+ main sxripts, independent modules, each of which is responsible for a specific task. This improves readability, simplifies debugging, and allows code to be reused in other projects.
-
a) Modules:
–each module must be a separate class, utility, function, or regular singleton (a class that will be used once. For example, any Service in luau is a singleton). In short, a module is not necessarily just a table, and it is important for you to remember this. In factA module is just a value that we return at the end of the code, usually it is a table.
-
b) Classes
–Each class is a separate module, even if it consists of only 2 fields. The file name must match exactly the class name (ZoneManager, BlasterClass) as far as I know, the module itself is usually returned when creating a class, however, until roblox fixes its TypeChecker (We’ve been waiting for this for a long time… Does it take 4 years?) you should use typing, and in my opinion, instead of returning the module table, return the constructor itself. -
c) Naming Conventions
camelCase
For variables and functions (for example, playerPosition, calculateDamage). The first word is written with a lowercase letter, each subsequent one is capitalized.
PascalCase
For classes (for example, PlayerCharacter, InventoryManager). The first letter of each word is capitalized.
UPPER_SNAKE_CASE
For constants (for example, MAX_HEALTH, PLAYER_SPEED). All letters are uppercase, words are separated by underscores.
–By the way, you should declare each service at the beginning of the code. Variables also, but in a reasonable sense (only those that are needed in the entire script, as an example - constants), with modules and classes as well as with variables -
d) Folders
ReplicatedStorage
: The code is stored here, accessible by both the client and the server.
—Remotes
: Scripts for RemoteEvents, RemoteFunctions (Bindables? Really idk)
—Client
: I think u alr know
— (Keep in mind that in order for the local script to run, you need to use the regular one with the RunContext change.)
—Services
: Classes and services that implement logic.InventoryService, ShopService.
—Utils
: Utils. (function GetZone() return Zone end) ... return GetZone
)
—Constants
: A module with constants (UPPER_SNAKE_CASE
!)
–
–
ServerScriptService
: Scripts that run only on the server are hosted here.
Don’t overload it -
e) Singletons: Use singletons to control access to
unique objects
or resources. Create them directly in the module, avoidingglobal variables
(roblox singleton is a… Like a service)
(Don’t forget that globals and getfenv disable optimization.) -
f) Tags and Attributes
As far as I know, you can create private and public values in unity. This is also implemented in luau (I’m not talking about the possibility of creating a privateValue table in a class, etc.), in roblox studio it is also possible, but this is done through attributes. (in the code, instead of knowing, you write obj:GetAttribute(“idk”)). In cooperation with classes, this creates a very cool logic. Also, you should use tags for better organization. In the code, you can write CollectionService:GetTagged(“idk”) and get a table of all instances with this tag at once
I’ll write the next part tomorrow. Good Luck
(Sorry for my bad English, I used translator)