155 Views • 103 Unique

Variables

Variables can be compared to a box, or container.
In our case, these "boxes" store a certain value, which we can then use in our code.

Variables can be "created" like so:

Loading...

Lets analyze this before we move on:
local for now, you can assume this keyword is used to indicate the creation of a variable. We will explore what this really means in a future tutorial.
Name is simply the name of our variable! You'll see how this comes into play in a second, but this is the name by which we can refer to the variable.
= the equals sign signifies that we are putting 'Value' inside the Name box.
'Value' is the value that we're putting inside the Name box.
This can come across as overwhelming, but you can always come back here in the future.

💡tip

"Creating" a variable is often referred to as "defining", which is how I'll refer to it from now on.

Now that we have a variable, lets change its value!

Loading...

As you can see, we haven't added local to the new line. This is because the Name variable already exists, so we can just override it instead of defining a new variable!

Primitive data types

Variables can have different types of values, which behave differently.
Usually Roblox handles this for you, so you don't need to worry about them, however it's good understand some of the basic ones, in case they're ever mentioned.
These basic types are referred to as "primitive" data types, because all other types are somehow related to these.

Strings

📝note

Like in the first example, strings can be defined like so:

Loading...

Strings have a ton of hidden features, which we'll go over in the future!

Numbers

📝note

The highest number that can be stored in a number is 18446744073709551615, any larger numbers must be a string!

Loading...

Roblox luau uses 64 bit integers/floats for numbers, if you're familiar with those terms.

Booleans (Bools)

📝note

Often simply called bools, booleans are a simple "true" or "false". On or off.
Booleans will appear often in future tutorials, and are more often used than you might assume.

Loading...

Nil

📝note

Called null in many other languages, it represents the absence of a value.
Simply said, nil means no value. Just like the other types, it can be defined like so:

Loading...

There are many other types that we'll go over in the future, because they are more complex. This includes, tables, functions, threads, and other Roblox-specific data types.

Printing

print() is a quick and easy way to see the value of your variable. If you create a new script, it will contain the following code:

Loading...

If we expand this a little, and create a variable, we can create something like this:

Loading...

Now, congratsulashions? Congrats! You've created your first variable. If you run your game, you should see the value of Variable appear into your output.

Naming variables

Naming your variables is not only a very important part of scripting, but also one of the most controversial parts.
If you remember to stay consistent, and name your variables according to their purpose, this won't be an issue.

Conventions

The "rules" around how your variable names are formatted is called a convention.
A few common conventions include PascalCase, camelCase, and snake_case. Roblox and myself both use PascalCase, however you are free to choose your own preference.

Here's a small demonstration for how this applies:

  • PascalCase: local VariableNamingConventions
  • camelCase: local variableNamingConventions
  • snake_case: local variable_naming_conventions

Common mistakes

wrong

Common mistakes to avoid:

  • Forgetting local when creating a variable.
  • Using illegal characters in variable names.
  • Starting variable names with a number.
  • Giving 2 variables the same name

Thanks for reading! I encourage you to try things out and practice yourself!

Written by @froggodoggo