Variables
In simple terms, a variable is something that can store something. In malls, you probably use a shopping cart to carry items and why do you do that? Because it is a much more efficient and easy to carry multiple items rather than individually carrying each of them. Variables serve the same purpose. It stores a value, making it easier to access later in your game.
Eg:
--Ignore the local, we'll come to that later.
local item = "Bread"
local friend = "Jack"
local school = "Roblox University"
Here you can see that item
is used to reference Bread
, friend
is used to reference Jack
and so on.
Why Use Variables
Why not just type Jack
or Bread
?
Variables make it easy to reference values, so instead of typing out a loooong name again and again, you can assign it to a variable:
local longName = "pneumonoultramicroscopicsilicovolcanoconiosis" --hmm thts a pretty looong word!
--With variable
print(longName) -- Very clean and easy to read and understand
--Without variable
print("pneumonoultramicroscopicsilicovolcanoconiosis") --the type of code that will drive you crazy
Different Types Of Variables
Now you might have noticed that we put Jack
under double quotes(this " "
thingy). That’s because in programming, there are different types of variables, just like how you would classify Milk as Dairy
or Eggs as Poultry
. You can’t go and put a chicken in the veggies section and call it lettuce. Similarly, you have different types of variables for numbers, letters, lists and dictionaries.
String
Strings are used to store letters or alphabets. While assigning a string value to a variable, you should put either single quotes' '
or double quotes" "
.
Example:
local breakfast = "Toast"
Numbers
Numbers are, well, numbers! Anything containing numbers including 0-9 falls under this category. You can also perform calculations with it.
Example:
local myFavouriteNumber = 911
local add = myFavouriteNumber + 89
print(add) --prints the sum of 911 and 89, which is 1000
lo
Now you might be asking, well then why not do
local myFavouriteNumber = "0" --0 is a string value here...
It is a way to do it, but here it is a String
, so if you try to do arithmetic operation such as multiplication, division etc… it will result in error. That is because strings were never meant to perform calculations, for that we use the number data type.
Bool
Bools are simple- true or false. That’s it, it defines whether a condition is true or false.
Example:
-- conditions made by my mom
local goToSchool = true
local eatCandy = false
Arrays
Arrays are like lists, like the ones you carry while going shopping. It stores multiple values which can be of any type.
Arrays can be defined by enclosing your desired values with curly braces ie:{ }
. Each value inside an array should be seperated by a comma.
Example:
local groceryList = {"Bread", 19, false} -- it contains string, num and bool
Dictionaries
A dictionary has a key and a value. Like arrays, dictionaries are created with curly brackets {}
.Different key value pairs are stored on separate lines followed by a comma.
Example:
local food = {
["Turkey"] = "Yummy!",
["Do you like veggies?"] = false,
["How many plates do you have?"] = 0
}
That’s it basically it! Congratulations! You just made your first step into becoming a front page game scripter!
P.S: This is my first tutorial and I hope to continue it further, so if you have any feedback, feel free to comment down below. Hope this tutorial helped you in some way
- This was a great tutorial!
- This is an okay-ish effort
- Nah, this is complete trash
0 voters