User:Mattchewy/LessonDemo
Introduction
Probably the most powerful part of any computer language is variables. In Lua, a variable is something that holds data and can be changed at any time. Let's pretend you have 3 apples. You want to get more apples, but your hands are full. So you put your apples in a basket while you pick more apples. The basket could be considered a variable. There are many variable types in Lua, but in this lesson we will only go over the three most basic types: Numbers, Strings, and Boolean variables.
Declaring Variables
To create a variable, you have to tell Lua what you want to name the variable and give something that the variable can hold. To declare a variable, you put the variable name in the script followed by an equal sign and then what you want the variable to hold. Here is an example following our apple example of declaring a variable:
apples = 3 newapples = apples + 5
In this example we declare that the variable 'apples' will hold the value 3. On the second line we declare that the variable 'newapples' will equal the value of the 'apples' variable plus 5. The value of 'newapples' will now equal 8.
Variable Types
As we said either, there are many types of variable types in Lua, but in this lesson we will discuss Numbers, Strings, and Booleans.
Numbers
A number variable holds basically what it states, a number. This number can be an integer, a decimal, or just about anything in between. Here is an example of a basic Lua number variable.
myint = 25 mydec = 3.14
In the first example, we create a number value of 25. In the second example we create a decimal number in Lua.
Strings
A string variable is a variable used to represent a set of characters. A string variable is started by a apostrophe, and then contains a 'string', or set, of characters, and ends with another apostrophe.
mystring = 'Look at my EPIC string!'
In this example we declare 'mystring' as a variable which holds the string 'Look at my EPIC string!'
Booleans
Boolean values, also known as bools, are the simplest Lua variable. A boolean value either holds the value of true or false.
mybool = true
In this example we declare 'mybool' as a boolean variable with the value of true.
Assignment Questions
In order to become great with anything you do, you have to practice. Open Roblox studio and code the problems in. Experiment with every aspect of it, and try something new.
- Create a variable named 'Bob' and make it hold the value 30.
- Take you variable 'Bob' and change its value to a string of your choice.
- Create a boolean variable which holds the value true.
- Create a new variable and make its value the same as the value of the variable 'Bob'
- Create a variable which holds your last name in it.