Beginner scripting tutorial #2 - Variables

Variables

Questions we will be answering:

  • What are they?
  • What’s the point in using them?
  • How do I use them?

Expected knowledge:

  • None

What are variables?

Variables are words used to (temporarily) store values in your scripts. I say temporarily as they do not save over different games.

What’s the point in them?

Variables are useful for a number of reasons:

  • Storing data (most relevant)
  • Stopping repeated code
  • Making scripts easier to read / understand
  • (In some cases) helping with memory / lag

How do I use them?

Variables have a name and a value. They key is the name, and the value is the value attached to it. For example, if you were making a gun, you might want a variable for the damage. The name could be called “Damage” and the value could be “100”. The way we make variables is simple - you write name = value like this:

Damage = 100

It should also be noted that variables can store any piece of data (value) that you want, so for example it can store strings (letters / words), numbers, floats (numbers with a decimal) and many other types, that we will get into at a future date.
As for the names of variables, you can use numbers, letters and underscores, so long as the name does not start with a number. So for example you couldn’t say:

123variable = 17

To actually use these variables in your scripts is easy; you simply write out the name of it, and you’re done! The script will automatically convert where you’ve used the name of your variable into the value.
You do not need to put any speech marks in around the word, just type it out as normal. Example:

text = "Hello world!"
print(text)

Memory usage

Variables tend to not take up a lot of memory, but can sometimes take up more than necessary. The way we counter this is by putting the keyword local in front of our variable names. You will not notice any difference at this point, but I will explain it during the if statements tutorial where we use indentations. But in short, the way you want to be writing your variables is like this:

local name = "value"

Concatenation

You may have also noticed that I mentioned something called “concatenation” in the previous tutorial. This is a way of joining variables and strings together. For example, when using print() you can only put one thing inside the brackets - but what if we wanted to print out a regular string and a variable at the same time? Doing something like this wouldn’t work:

local name = "Jimmy"
print("My name is name")

Because it would just print out “My name is name”. This is where concatenation comes in - to seperate the string and the variable. The way we use concatenation is by first ending the string (having both speech marks in place, i.e. “my string” then using two dots (full stops, periods…) a space and our variable name. Like this:

local name = "Jimmy"
print("My name is".. name)

And this will print out My name is Jimmy as expected.

Useful tip: Name your variables appropriately

A common mistake seen in new programmers is that they don’t name their variables well. By “well” I mean they don’t give them names that make sense… for example, if you were making a variable for the damage of a gun, don’t name it something random like “aaa = 100”. This will just make reading and understanding your scripts a lot tougher.


Now that you’ve learnt about variables, have a go at testing your knowledge on these questions:

Question 1 (Easy)

How would I create a variable called Points with a value of 100?

Hint: The way we create variables is by writing local name = value
Answer:

local Points = 100
Question 2 (Medium)

How would I make a variable called food and print out “I like food name”?

Hint: Make sure you’ve ended the string properly, used two dots and spelled the variable correctly
Answer:

local food = "Pancakes"
print("I like ".. food)
Question 3 (Hard)

How would I store the sum of two variables (what variable + variable is equal to) in another variable, then print it out?

Hint: We use an arithmetic - the plus sign (+). - This is what we’ll look at in the next tutorial
Answer:

local num1 = 5
local num2 = 3
local answer = num1 + num2

print(num1.. " add " ..num2.. " = " ..answer)
20 Likes

wow, i understood this very well. I was able to answer all 3 questions correctly! Thank you for this.

5 Likes

Idk man i like writing game.Workspace.Instance plenty of times rather than just storing them in a variable - DON’T ASK QUESTIONS!

Examples of what i mean:
I feel like writing:

game.Workspace.Part.BrickColor = BrickColor.new("Black")
game.Workspace.Part.Name = "settingPart"
game.Workspace.Part.Position = Vector3.new(3,3,3)

Rather than:

local changingPart = game.Workspace.Part
changingPart.BrickColor = BrickColor.new("Black")
changingPart.Name = "settingPart"
changingPart.Position = Vector3.new(3,3,3)

Understandable tutorial tho.

4 Likes

I don’t think this is the proper way describe the difference between local and global variables. It doesn’t really solve a memory issue as much as it solves a scope issue. I realize you haven’t introduced the idea of a scope yet, but instead of saying something like this maybe describe their differences in terms of where they are stored in memory (global variables on the heap and local variables on the stack).

2 Likes

Yeah, in some cases it was fairly hard to describe considering this tutorial is aimed at people who know at most how to use print()
I was also partially referring to putting functions in a variable, i.e. local newVector3 = Vector3.new but thinking about it now maybe it would have been best to leave that out

Post is an okay start, but I think it’s missing a few critical components. Namely things like:

  • Variable scope (you did try to reference this to some extent, but it really belongs to this post)
  • When a variable should be used (no magic numbers in code) and when it could be skipped
  • Variables have their own independent memory. You should show how setting one variable to another doesn’t make their values linked (except shallow-copied things like tables).
  • The section Useful tip: Name your variables appropriately should be expanded on more. It’s vital that you explain the importance of readable code to beginners so they learn good practices early.

Other feedback:

Critical reason missing here - as a storage place for data. The “Question 3 (Hard)” section is a prime example.

And underscores - often used in OOP.

This line doesn’t really make any sense and isn’t true. A variable will take up just as much as memory as the data it references (exception with shallow-copies). Personally, I wouldn’t mention memory at all until you introduce scoping.

IMO, this section shouldn’t be in a variable tutorial. The concat operator should be explained in an operators section/tutorial.

1 Like

I did try to mention what I thought was necessary, but I also didn’t want to completely overload people with information - I’m still not absolutely sure to what extent of information I should have given, but I do agree not everything was said.

Yeah, I’ll change that.

^ This too

By not a lot of memory, I meant by what people reading this would try to store for testing purposes, i.e. a small number. And again, partly down to me not knowing how much information should be given at this stage.

Yes I was considering putting this into a separate post too, though I mentioned it in the previous tutorial so felt like I should include something about it here.

I understand, but beginners are unlikely to know anything about memory. With that said, there’s no reason for you to stress them about it.

2 Likes

I think it would be beneficial to introduce scope at a very basic level alongside local variables. The do statement is perfect for this because it’s simple to use and understand.

local a = "example"

do
  local b = 27
end

Our of most tutorial I find loops easier. I think learn loops as beginner is easier.