AOTR complete guide beautiful and efficient code

Hey there, this will be a complete guide to how the scripting in AOTR is going to take place.
This also applies to other projects but I am going to make it specifically for AOTR developers.

This will be going over

  • Indentation
  • Formatting
  • Coroutines
  • Module Scripts
  • Functions / Variables
  • Event Usage
  • Elseif / Else

The format will be

  1. What to do
  2. Example
  3. Reason
Indenting
  1. Use only 4 line indenting and use it only once. If you are using an editor that does not do this, you can paste it in a Roblox script, right-click it, and select format script so that there is no indentation problem.
if true then
    print("true")
end
  1. Just don’t want auto indenting problem on Roblox
Formatting
  1. To format properly, arrange everything in the script according to how I show it.
    Leave enough whitespace in between to easily differentiate it.
-- services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

-- Object Variables
local Thing = ReplicatedStorage.Thing

-- Numerical Variables
local AmountOfCables = 2

-- functions
local MakeCables = function()
    for I = 1, AmountOfCables, 1 do
        Thing:Clone()
    end
end

-- Connecting and all the other stuff
Thing.Touched:Connect(MakeCables())
  1. Major aspect of making code look good. You mostly already know this.
Coroutines
  1. It’d be better to say that delay, spawn, and wait are all deprecated functions, and usage is completely superseeded by task.delay, task.spawn / task.defer, and task.wait respectively
local MovePlayer(Character)
    local BodyForce = Instance.new("Bodyforce", character)
    wait(1)
    BodyForce.Force = Vector3.new(10000,10000,1000)
end
local MoveCoroutine = coroutine.create(MovePlayer)
coroutine.resume(MoveCoroutine)
  1. You may have to write a few more lines but its all worth it
Module Scripts
  1. Module scripts are awesome, easy to use, and way too efficient to avoid. Use them everywhere needed. There are hardly any cons to module scrips.
local Module = require(script)
Module.Execute()
  1. Modular programming is key to efficient and organized code, so its usage is essential.
Functions and Variables
  1. Even if something is one use throw, use variables for it when coding. And functions, whether or not it’s 1 use, or multiple-use USE functions for it. Every small thing that can be differentiated from the rest of the code, MAKE IT A FUNCTION. Don’t worry about memory as it’s insignificant. Also, Naming the functions is really important. DO NOT name it TS or some short form that no one will understand. Make your function / Variable name Understandable enough and up to the point.
local ReplicatedStorage = game:GetService("ReplicatedService")

local ArrangeEvent = ReplicatedStorage.Remotes.Arrange
local Main = ReplicatedStorage.Main
local MainChildren = Main:GetChildren()

local ArrangeMains = function()
    for i , v in pairs(MainChildren) do
        local MainClone = v:Clone()
        MainClone.Parent = game.Workspace
    end 
end

Arrange.OnServerEvent:Connect(ArrangeMains())
  1. Using functions and variables plays a major part in the making of beautiful code and sometimes efficient code too. If you want to change any part, you don’t have to scroll through thousands of lines of code to find what you need.
Event Usage
  1. Even tho this is pretty common, always look for event connection execution.
local RunService = game:GetService("Service")

local UpdatePosition = function()
    --do stuff
end

RunService.HeartBeat:Connect(UpdatePosition())
  1. It’s just efficient, what can I say.
Elseif Spam
  1. No elseif spam anywhere. Instead, use tables (Dictionaries) to link any function to a variable or a string as shown in the code below.
local UserInputService =  game:GetService("UserInputService")

local Movement = function(position)
    -- Moves stuff
end

local InputToFunctionLink = {
    Enum.KeyCode.A = Movement(LeftVector),
    Enum.KeyCode.D = Movement(RightVector)
}
local function onInputBegan(input, gameProcessed)
    InputToFunctionLink(input.KeyCode)
end

UserInputService.InputBegan:Connect(onInputBegan)
  1. Elseif spam causes something known as the “branching” problem. This can cause lots of delay which again, for a movement-filled game is WAYYY too dangerous.

If you want to make your code really really good and efficient, I recommend you read this Lua Style Guide

Just to conclude, if your code looks something like this,

you know you are doing something wrong and there is a way you can improve your code a lot.
Also, no need to be ashamed of your code looks something like this, everyone’s been there at some time. Me, like a few months ago.

Now about the logic of the code itself, never compromise logic just to complete code faster. Instead, we encourage you to share the logic with others before you code so that if someone has a better way of doing it, he can tell you. And if needed you can even create diagrams to decide the logic before coding it.

expect more content to be added over time so do follow this topic

2 Likes