Guide on making scripting cleaner and easier to understand!

Ok so I am a messy scripter and some of yall maybe be too i suggest yall making cleaner scripts for example:

Part 1: Use Of Variables

:x:

script.Parent.Parent = script.Parent.Parent.Parent

This line above does look messy and for newly devs or non devs it is hard to understand

:heavy_check_mark:

local Frame = script.Parent

local MainFrame = script.Parent.Parent.Parent

Frame.Parent = MainFrame

The code above this is easy to understand and can be easily manipulated

Part 2: Spaces

So the problem I face is when i make a huge code and when I have to change one thing i have to go find the line of the code to go change it (and yes i know there is something called find in roblox but i dont use it often)

it would be better using spaces

:x:

local event = game.ReplicatedStorage.event
local part = instance.new("Part")
part.Name = "Child Of Workspace"
part.Parent = game.Workspace
part.Anchored = true
part.CanCollide = false
part.Size = Vector3.new(10,10,10)

The code above this is a bit messy you know

:heavy_check_mark:

local event = game.ReplicatedStorage.event

local part = instance.new("Part")

part.Name = "Child Of Workspace"

part.Parent = game.Workspace

part.Anchored = true

part.CanCollide = false

part.Size = Vector3.new(10,10,10)

The code above this is a better compared the the one with the :x: above it

Part 3: Comments

Navigating can be tough in scripting luckly we have comments!

-- Hi! My name is comment. Nice to meet you fellow user!

Scripts like this one
:x:

local event = game.ReplicatedStorage.event

local part = instance.new("Part")

part.Name = "Child Of Workspace"

part.Parent = game.Workspace

part.Anchored = true

part.CanCollide = false

part.Size = Vector3.new(10,10,10)

Might be easy but when you have bigger for almost 30-1000+ lines of code then comments are there to save your day!
Especially comment come in handy when making tutorials like this one!

:heavy_check_mark:

local event = game.ReplicatedStorage.event -- Event

local part = instance.new("Part") -- Create new part

part.Name = "Child Of Workspace" -- Name our part

part.Parent = game.Workspace -- Parent our part

part.Anchored = true -- Anchor our part

part.CanCollide = false -- Make our part be able to phase through any object

part.Size = Vector3.new(10,10,10) -- Make our part a GIANT

The line above me is easy for newbies or non scripters to understand!

Part 4: Function Junction

So you may have heard of:

local part = script.Parent -- Part

part.Touched:Connect(function() -- Touch Function

  -- Do Something

end) -- End

A very common function but there is a way around this and make it clean!

Fo example:

local function OnPlayerJoined(Player)

print(Player.Name) -- Print Name

end)

game.Players.PlayerAdded:Connect(OnPlayerJoined)

Now time for customizing

You can make custom function in roblox too!
Here is a script without a custom function:

:x:

local Decimal = 1.246810 -- Decimal number

print(math.floor(Decimal)) -- Rounding to the lowest value

Prints:

1

Heres one with a custom function:

:heavy_check_mark:

local Decimal = 10.75 -- Decimal number

local function RoundDown(DecimalNumber) -- Custom Function

  return math.floor(DecimalNumber) -- Returns our number

end) -- end

print(RounDown(DecimalNumber))

Prints:

10

Oh wow you made it to the end :tada: Congrats! :tada:

Hope this helped and please do not post any hate comments or report this was a family friendly tutorial for new players or players who want their script to be cleaner

Good Bye! Stay Safe

27 Likes

This is not a bad resource! Keeping the code clean is an important part of a developer and it sure isn’t that easy as it seems, but many of the things you include here are already found on the amazing Roblox Lua Style Guide, from my point of view the best resource to learn how to not only keep code in roblox clean but also keep it in sync with the defaults of roblox to normalize all codes and make everything more global and easy to understand.

There are some other resources in the forum really good like a discussion talking about good and bad practices.

11 Likes

Few problems I found.

Its the same thing, but the code you marked as incorrect has better code formatting than the one you marked as correct.

image

There are an immense amount of unnecessary white spaces in between each line.

Here, use of Pascal case is better than having white spaces.


The 2nd image isn’t really a custom function since you are running the same math.floor function inside the RoundDown function, which is unnecessarily increasing lines of code.

The output is correct. But math.floor() doesn’t round a number, instead use math.round(10.75) which results in 11 being the output.

13 Likes

Using much local arent to good for client memory usages on game

1 Like

It is. It just doesn’t show up with the autocomplete of the studio editor.

2 Likes

I mean, even though it was family-friendly (which literally every tutorial on the forums are), this really wasn’t a good tutorial. It’s terribly formatted and hard to read because of improper grammar


This isn’t true. In fact, I’d prefer the first one because it’s more compact and* has more readability. Everything can be seen at a glance which the second option doesn’t provide.


This is not necessary, as it clutters the code. Not only are you using more time naming every single line of code, but you’re also hurting readability, as comments are highlights for important things that you don’t want to forget or a little explanation of what a complex thing does. It’s also better practice to state why that line of code is there than what that code does (in most cases), which you didn’t do.


This isn’t necessary. This isn’t bad to do, nor is it essential. This part of the tutorial is up to preference. Functions might be cleaner for some, but not for others.


What’s the point of making a custom function? math.floor literally explains itself. Not only are you making code longer, but you’re also hurting performance.


I understand that you want to provide resources for newbie developers, and I congratulate you on that, but I don’t think this would benefit newbies as much.

12 Likes

Hmm. I am very skeptical of this.

Especially Comments. Usually, when it comes to writing clean code the goal of the code is to make it understandable If you already understand it then nothing needs to be changed.

In you example you gave a comment for each line.

It is not only completely unnecessary to have each line with a comment but
it is also time consuming. you only really need comments to “sectionize” idrk the real name your code.

I do agree with variables definitely. I see too many people put char instead of Character or UIS instead of UserInputService I think good variable and parameter naming is a good idea.

The spacing makes it weird and unnecessarily big when writing lines, since you’re adding an entire extra line for each line of code.

1 Like

This is a good tutorial, although I disagree on the space part

I disagree and agree at this, yes you should add spaces on your code so it can be readable, but not too much, what i think you should do if a line and below lines are related you don’t need a space,
here is an example:

local part = instance.new("Part")
part.Name = "Child Of Workspace"
part.Parent = game.Workspace
part.Anchored = true
part.CanCollide = false
part.Size = Vector3.new(10,10,10)

local function OnPlayerJoined(Player)
print(Player.Name)
end)

game.Players.PlayerAdded:Connect(OnPlayerJoined)

Something like this should be readable and doesn’t use unnecessary amount of spaces on the code.

edit: some grammar fixing

Part 4 can vary sometimes depending on what you want.

For example, if you’re going to call the same function multiple times, then make a function for that.
But if you’re only going to call a function once, then I would probably go with that.

This is less readable than the “messy” code. Here is a better example:

--Services-- (variables should be in PascalCase)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

--Global Variables-- (Should be in camelCase)
local epicPart = Instance.new("Part")

epicPart.Name = "EpicPart"
epicPart.Anchored = true
epicPart.CanCollide = false
epicPart.Parent = workspace

I agree that comments are useful when working with multiple scripters or making a tutorial. However, I feel like it is a waste of time if these cases don’t apply to you.

Why increase the amount of work put into it? Just use the inbuilt functions instead of creating your own. (can also take up more memory)

4 Likes

I don’t understand the issue with this code tho. If you don’t understand what math.floor does when you look at it, then that probably is a sign that you need to go read the docs. Another thing is that doing this

is wasteful. For one, there’s too many comments. Probably better off with one comment on the function declaration telling you what it does. Also, because Lua has first class functions, you can assign functions to variables like any other datatype. Like so:

-- Rounds down the input number: rounddown(1.1)
local rounddown = math.floor
print(rounddown(1.1)) --> 1

personally I think this is better

local Frame = script.Parent
local MainFrame = Frame.Parent.Parent

Frame.Parent = MainFrame

as you can see I used Frame when defining MainFrame

local event = game.ReplicatedStorage.event
local part = instance.new(“Part”)

part.Name = “Child Of Workspace”
part.Parent = game.Workspace
part.Anchored = true
part.CanCollide = false
part.Size = Vector3.new(10,10,10)

this is how I think spaces should be used

This tutorial is very bad for people learning to code, you introduce one more space per line of code which in return will just make it look unreadable at high amounts of lines.

Another thing is that you shouldn’t be making your own function, just use math.floor (the custom functions don’t give any performance increase and in fact do the opposite.)

Your example of the comments is very bad, you shouldn’t be putting a comment for every single thing you’re doing, only put them sparingly so the user who tries to read the code doesn’t get confused with all the comments.

The last thing, you should never be putting .Parent at the beginning of a new part, due to it having to do some stuff, instead you should ALWAYS put .Parent at the end of the property changes.

1 Like

the tutorial acc is also dead idk the point of replying to this crappy tutorial. and not gonna read your whole reply + when I wrote that tutorial I was dumb af in scripting.

The fact that this is created just to make code cleaner is just something else…
Making code clean in terms of readability is on you, I don’t think people will post hate comments, probably will be criticism to improve yourself.

I disagree with having unnecessary empty lines in each line of code, imagine scripting a framework with that method.

I just noticed that this is a old topic lol

the tutorial is outdated bruh I made this when I joined devforum and didnt know much.

Doesn’t look bad but it sure got flaws!

Could be:

local Frame = script.Parent
local ParentFrame = Frame.Parent
local MainFrame = ParentFrame.Parent

Frame.Parent = MainF

Could be:

local event = game.ReplicatedStorage.event

local part = instance.new("Part")
part.Name = "Child Of Workspace"
part.Parent = game.Workspace

part.Anchored = true
part.CanCollide = false

part.Size = Vector3.new(10,10,10)

This is nitpicky but improper indentations:

local function OnPlayerJoined(Player)
    print(Player.Name) -- Print Name
end)

game.Players.PlayerAdded:Connect(OnPlayerJoined)
1 Like

The OP is terminated so there will be no one to fix these