My Script Template - For Everything

I’ve come to realize in my time learning Roblox development that many people do not actually organize their code much and just go line by line. I however feel much nicer when I code to have a strict template that I follow. Every single one of my scripts I ever write has this template and I’ll explain why!

The template:

--[[SERVICES]]--

--[[FOLDERS]]--

--[[MODULES]]--

--[[TYPES]]--

--[[VARIABLES]]--

--[[FUNCTIONS]]--
--Function Declarations

--Function Definitions

--[[SCRIPT]]--

Example of a Filled out template:

--[[SERVICES]]--
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
--[[FOLDERS]]--
local SS_Modules = ServerStorage.Modules
--[[MODULES]]--
local DataHandlerModule = require(SS_Modules.DataHandler)
--[[TYPES]]--
type PlayerProfileType = DataHandlerModule.PlayerProfileType
--[[VARIABLES]]--
local PlayerProfileList: {[Player]: PlayerProfileType} = {}
--[[FUNCTIONS]]--
--Function Declarations
local PlayerAdded: (Player: Player) -> ()
local PlayerRemoving: (Player: Player) -> ()
--Function Definitions
PlayerAdded = function(Player: Player)
        local PlayerProfile: PlayerProfileType = DataHandlerModule.GetPlayerProfile(Player)
        PlayerProfileList[Player] = PlayerProfile
end

PlayerRemoving = function(Player: Player)
        PlayerProfileList[Player] = nil
end
--[[SCRIPT]]--
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

The order in which the template is made is based off of what you would need when filling in code for each section. For example, in order to write your functions, you need your variables, sometimes you also want to have specific types for those variables. Those types can be from module scripts, so module scripts have to come before. In order to reference the module scripts you need to be able to reference where they are, aka their folder. Then finally in order to reference the folder you need to reference the Service.

I realize this may seem unnecessary to some of you as most people generally code like this just without actually explicitly writing it out, but maybe having the explicit Template will help you like it helps me.

For people curious as to why I have Functions Declared and Defined is because I like to alphabetize my functions for my own readability so I have an easier time navigating. Although you can probably assume that alphabetizing makes functions hard if you call functions that are alphabetically before the one in use, therefore declare all of them beforehand and you are good to go.

Let me know what you guys think and what is your personal tips and tricks you do to keep your code Clean and Readable.

I will also add that I am a “Never Nester” and love type checking wherever and whenever possible.

17 Likes

I suppose I’ll share mine too!

local Super = {}
local Object = {}
Object.__index = Object

--\\ Variables

local player = game.Players.LocalPlayer...

--\\ Templates

local Object = script.Object...

--\\ Types

export type Object...

--\\ Private Methods

local function method()...

--\\ Public Methods

function Super.new()...

--\\ Public Instance Methods

function Object:Method()...

--\\ Private Instance Methods

function Object:_Method()...

--\\ Connections

game:GetService("RunService").RenderStepped...

return Super

Not all of these categories are used in my scripts. Variables are always first, but I don’t label those unless I have a significant amount of code above those. I also provided an example of code under those categories.

6 Likes

mine is like what except I do

-- Services
-- Modules
-- Libraries
-- Variables

however ima start using the – Private Methods and – Public Methods like blueberry showed

2 Likes

I organize my code similar to you but personally i use “–//” so its like

--//Services

--//Folders

--//Modules

--//Variables

--//Functions

Interesting but just apply that …

1 Like

The template I follow goes something like:

-- Script Name
-- Username

-- Services --

-- Variables --

-- Functions --

-- Events --

-- Calls --

Now sometimes you may need to expand this or even shrink it, but that’s why I love using this template. It’s very flexible for many cases you may need!

My module template for example just adds another line with -- Returning -- at the very bottom (For returning the module table exclusively)

2 Likes

I’m confused by what you are trying to imply

2 Likes