Variables for beginners (Part 1 of scripting tutorials)

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:

  1. Pascal case
local MyVariable = "chicken"
  1. Camel case
local myVariable = "chicken"
  1. 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)

2 Likes

Better name recommendation: How to define a variable?

You forgot to mention the _G one, mention it and say that it’s better to use module scripts, just so the reader knows if (s)he came across it any time.

Forgot numbers in your list. (You can not use numbers as the first letter of a variable.

It’s good practice to name variables using camelCase, classes using PascalCase and constants using LOUD_SNAKE_CASE, also called ALL_CAPS

task.wait(4)!

Great tutorial nonetheless!

3 Likes

I dont think ill be adding that, I feel like that is too advanced for such a simple subject. I will probably make a whole post about module scripts. (I still dont completely know how they work haha)

Also thank you for the input, I really appreciate it. I updated the post accordingly

Edit: adding _G explanation tomorrow

It’s still variables but alright.

Oh! I meant the _G part, no need to explain Modules in here.

If you have any questions fell free to ask!

Your welcome!

(For further talking, Pm or Dm me so we don’t fill the post.)

1 Like

This is a very Basic Tutorial, Its just a very long way to say:
"Variables Store Objects and Information"

This can Basically be found on YouTube

How to SCRIPT on Roblox #2 - Variables - YouTube
VARIABLES - Roblox Beginner Scripting Tutorial #5 - YouTube
Variables - YouTube
Variable Names - YouTube

People are Recommended (even by Roblox themselves) to look for the info before creating topics on the DevForum, which is one of these cases.

I said in the post its self…This is an absolute beginner tutorial

Still doesnt justify it, they even say to not post basic tutorials here, which im not sure what you are even doing for.

There are plenty of ways you can create a basic tutorial, but here roblox doesnt want you to do this as there are plenty of them outside the Forum. And ones they have created themselves to help people.

  • Make sure you read the Rules of the Developer Forum’s Community Tutorials Category

  • Make sure you look for the Information before creating topics.

I am going to continue making these absolute beginner tutorials. If you have a problem, contact a DevForum administrator or report my post. I’m not stopping making these just because someone got mad that I tried helping the community.


I’m not mad, I’m saying there are other Resources that do the exact same thing, And “Dont Post Basic Things” is listed on the Rules:

or Lua (many basic “learn Lua” topics already exist).

There is literally nothing wrong with helping people with basic things, I’m just saying Community Tutorials isn’t that place (even the rules say so)

I understand where you are coming from. In the future I will take a deeper dive and make the information apply to even experienced developers.

1 Like

First of all, this is a easy tutorial that you can easily find on Youtube / the Roblox Blog.
Secondly, Be aware that this tutorial has high chances of being deleted.