There is no doubt that Scripting is one of the most valuable aspect in Successful Games and arguably one of the most important skill to learn in Game Development.
In this Thread I would like to share my experience, guides, tips & tricks from my years of Scripting; I have freelance, collaborate with Developers, teach new scriptures and currently teaming up with a Family.
Naming Variables
I've seen a lot of bad code with terrible Variable Names
Here are a few Examples;
local myCharacter
local myHumanoid
local myPlayer
There is no reason to use âmyâ itâs very unnecessary and adds 2 extra characters but that I can deal with that, itâs not a huge problem.
What really upsets me are poorly named Variables
sp=script.Parent
local chr=sp.Parent
local t=chr:FindFirstChild("Torso")
local h=chr:FindFirstChild("Humanoid")
local p = Instance.new("Part");
local w = Instance.new("Weld", p);
local f = Instance.new("Fire",p);
No one not even the Scripture would know what âp, w, f, h, t, chrâ means without context!
There is nothing wrong with compressing and making your code short but please keep itâs integrity.
__
So how can I improve my code's readability and integrity?
Put some thought into naming your Variables, make sure it explains itself well without context
The same rule applies to Functions, Arguments and Parameters
-- VARIABLES
local Color3_New = Color3.new
local UDim2_New = UDim2.new
local CFrame_New = CFrame.new
-- FUNCTIONS
local function GetCharacter(Part)
end
local function RecalculateHipHeight(Character)
end
local function ScaleModel(Model, Scale)
end
local function CharacterAdded(AddedCha)
end
Guides recommended by readers:
Roblox Lua Style guide
Lua Style Guide
Lua Rocks Guide
Disclaimer : Not everyone has to do this, itâs a personal style
I usually do Name_Func and Name_Event to give it an extra identifier
Indenting Code
Not indenting code is a sin
Please donât to this
local function Name_Func()
if true and true or true and not true then
while wait() do
do
repeat wait() until true
for i = 1, 10 do
local Table = {
Name = "John",
Age = 10,
Gender = "Male",
Job = "Banker",
}
end
return
end
break
end
end
end
Do this
local function Name_Func()
if true and true or true and not true then
while wait() do
do
repeat wait() until true
for i = 1, 10 do
local Table = {
Name = "John",
Age = 10,
Gender = "Male",
Job = "Banker",
}
end
return
end
break
end
end
end
__
Disclaimer : Not everyone has to do this, itâs a personal style
Something that I do is Indenting based on Hierarchy
-- SERVICES
local PlayerS = game:GetService('Players')
local plr = PlayerS.LocalPlayer
local Backpack = plr:WaitForChild('Backpack')
local Cha = plr.Character or plr.CharacterAdded:Wait()
local Hum = Cha:WaitForChild('Humanoid')
local RootPart = Hum.RootPart
local Mouse = plr:GetMouse()
Creating Sections
An easy way to make your code easier to read is by creating Sections
-- SERVICES
-- MODULES
-- VARIABLES
-- TABLES --
-- META TABLES & META METHODS --
-- FUNCTIONS
-- SCRIPTS
--[|Remote Events|]--
--[|Remote Functions|]--
-- LOOPS
Tables
Tables are very versatile, powerful and used everywhere
Tables have many forms, it can be an Array, a Dictionary, a Mixed-Table, or even a Metatable.
Learning and Understanding Tables is massively beneficial because not only that you can make certain Services work better but sometimes it requires related knowledge, notably;
DataStoreService, RemoteEvent / RemoteFunction, MarketplaceService, UserInputService, TeleportService, and many many more.
This should help you understand how Tables work by using print()
and you reading the output
It also allows you to write less code for the same if not better results
Use Modules
Having too many Scripts in your game is a sin
You have at least one Script for each Object, but don't worry I've done this too when I first started Scripting
The problem of having too many Scripts are;
-
When you want to change a behavior of Game Objects you have to change every Script in them
-
Itâs confusing while debugging
-
It might impact performance
The main idea here is you should not have too many BaseScripts
__
Fixing this is simple; learn how Tables and Modules work, have one Main Script each for the Client and Server then create a Module for features that are related to each other, for example;
I have one ServerScript, one ClientScript
I have a ModuleScript for MarketplaceService, PlayersData, RemoteInstance and other stuff.
- This is handy because when I finish Scripting a Feature I donât have to look at it again until itâs necessary, code is easier to find, more organized, more readable and easier to debug.
Not using Operators
Utilizing Operators will shorten your code and increase readability
I have seen many Scriptures do this
if FirstBool == true then
if SecondBool == false then
end
end
But you can do this instead
if FirstBool and not SecondBool then
end
-
Only use â==â and â~=â when you want precision
-
You can use âandâ, âorâ instead of creating unnecessary Nested If Statements
__
MusicBtn.Color3 = On and Green or Red
- Use Ternary Operator instead of creating an If Statements
Understanding RemoteEvents and RemoteFunctions
Before using Remote Instances you must have an understanding of Server - Client Model, however a simple way I like to put it is
- RemoteEvents Fires an Event on the Server to run code via the Client and vice versa.
- RemoteFunctions Calls a Function on the Server to run code via the Client and vice versa and returns a value to the Invoker.
If you still have trouble understanding RemoteEvents & RemoteFunctions you can play around with BindableEvents & BindableFunctions.
Dealing With Exploits
Most of the time your game is Exploitable because of poorly written code
In a FilteringEnabled game the only thing that is Exploitable are poorly written code implemented into RemoteEvents and RemoteFunctions, if you donât give the Client power and always do Sanity checks on the Server there is nothing youâll have to worry about.
Itâs been said many times âNever Trust The Client!â
Stop Using JSONEncode Wrong
I have seen a lot people use JSONEncode to send a Table to DataStoreService, MessagingService and RemoteEvent / RemoteFunction
The truth is you donât have to, Roblox automatically does this so what you are doing is redundant and slow.
You only have to use it for GetAsync, PostAsync and RequestAsync
Minimizing Lag
You need to optimize your code because not all players have a modernized state of the art device, some might be using a Toaster to play your game.
Do not fall into the rabbit hole, do not over optimize your code, donât be a perfectionist be a person that gets things done.
A lot of people will say âIf you donât have a Performance problem donât try to fix it, donât optimize your codeâ
They might be right but I am not one of them, I rather prevent it from happening than fixing it later especially when you have a FrontPage game, but minimizing your Script Activity / Usage will increase Performance only when done right, however keep in mind that Latency is something you canât avoid but can minimize, however this is often times not caused by your code.
Recommended by Readers:
You can also use the MicroProfiler to analyze and debug performance problems.
Using Loops Instead Of Events
Only use Loops when they are necessary, most of the time you can use an Event
Debugging
- Use print() print() will always tell the truth
- Read the Output Window
Recommended by Readers:
You can also use the Lua Debugger to inspect your code more indepthly.
Thanks For Reading!
Thank you for reading, hereâs my gift to you Simple Scripts For Beginners
If you have any questions Iâll do my best to answer it, and please let me know if you have any issues.
Have fun Developing - Sincerely RuizuKun_Dev