Variables
Hello fellow developers! I am Powerful, I will be doing a series of absolute beginner tutorials. If there is any key information you think I left out, let me know.
This is part 1. Today we go over the following about variables:
How do I define a variable?
General naming
Good question, it is really simple. You type the name you access the information by, following that a equal sign “=”, and then the information. More on this later. For example:
MyVariable = "chicken"
Usually though, you say local before the name of the information, this makes the client access the the information faster. There is really no advantage of not adding local before the name of the information. If you want to know what this looks like:
local MyVariable = "chicken"
The cannots of naming
Alright, good. Now we know how to type them, let’s go over what we can’t type in the names. It is all the following:
- and
- for
- or
- break
- function
- do
- if
- return
- else
- in
- then
- elseif
- true
- end
- nil
- until
- false
- not
- while
These words are already taken up by Luau. We will go over these in other tutorials. You cannot use spaces " ", in the names of your variables. You also cannot start the names of your variable with numbers. Having numbers in the name is fine, unless it starts with it
Capitalization of naming
It is always best practice to use correct capitalization in the name of the variable, for example:
local myVariable = "chicken"
instead of
local myvariable = "chicken"
It helps you be able to read the script, and it also just looks alot better. My favorite type of capitalization in coding is pascal case, and the most common and the most used is camel case. Keep in mind, there is other types of capitalization. If you want to know the names of the different types of capitalization, here is what they look like and what they are called:
- Pascal case
local MyVariable = "chicken"
- Camel case
local myVariable = "chicken"
- Loud snake case
local MY_VARIABLE = "chicken"
The data
Finally, the juicy part of variables. This is the data/information the name we just created holds. This information can be strings, nil, booleans, numbers, tables, etc. If you want to know all the data types, I recommend reading this very informative post by XOLT1268. This part of variables is definitely the most complicated, (even on such a simple subject) but I cant write a whole book about data types in a post about variables. If you need to know anything, just message me, or comment here.
How do I use them?
Making the code shorter
The most common (and almost the only) reason to use variables is to shorten code. Here is an example:
local MyVariable = "Yes this is a string, I am making it long just to prove a point"
print(MyVariable)
task.wait(4)
print(MyVariable)
See that code above? I was able to reference MyVariable
twice. If I was not to use a variable, I would have to write down that big long string 2 times. Here is what that would look like:
print("Yes this is a string, I am making it long just to prove a point")
task.wait(4)
print("Yes this is a string, I am making it long just to prove a point")
Getting services
Another, less common use for variables, is to get services. If you want to understand what that means, I recommend watching this video by AlvinBlox. Basically, it gets the service at the very start of the script, and it only has to get the service once, making the script faster, it also shortens the code a little. Here is an example:
local MarketplaceService = game:GetService("MarketplaceService")
Sadly, I wont be writing more about getting services with variables because that is going to make this long, and is going to get off topic. I will make a tutorial on services in the future
I am planning on adding more to this in the future. This is just a very small look into variables (still better than the one written by roblox themselves)