Variables
Variables in Lua can be thought of as shortcuts. You can assign values to them, and access them with scripts. To set a variable, just assign a value to what you want the name of your variable to be. Every variable's value is equal to nil before you set it. We'll begin by creating a new variable:
x = 1
That was easy enough, right? We have a variables, x, with a value of 1.
You can name your variables almost anything you want, for example:
noodles = workspace.Noodles. To change a variable, simple assign it another value the same way you assigned the first. There are a few reserved words that you can't name your variables, though. They include: and, break, do, else, elseif, end, false, for, function, if, in, local, nil, not, or, repeat, return, then, true, until, and while. Variable names can only include letters, numbers, and underscores.
More with Variables
Lua is case-sensitive. This means that Hello, hello, HELLO, and HeLLo are all different things. Remember this. You could make a variable named While, just not while.
So you might be wondering when you're actually going to use variables. You'll probably be using them in functions and loops later, but they can be very handy in other places too. Say we have a brick in a model, and we want to change its properties.
brick = game.Workspace.Model.Model.Part
brick.Name = "My brick"
brick.Color = BrickColor.new("White")
brick.Position = Vector3.new(5,2,7)
brick.Size = Vector3.new(10,10,10)
Get the idea? You didn't have to type game.Workspace.Model.Model.Part every time you wanted to change a property. This can save a lot of time.
Another way to save time is by declaring variables like so: a, b, c = 1, 2, 3. In this example, a = 1, b = 2, and c = 3. See how that works?
Further reading: ROBLOX Wiki, Programming in Lua
x = 1
That was easy enough, right? We have a variables, x, with a value of 1.
You can name your variables almost anything you want, for example:
noodles = workspace.Noodles. To change a variable, simple assign it another value the same way you assigned the first. There are a few reserved words that you can't name your variables, though. They include: and, break, do, else, elseif, end, false, for, function, if, in, local, nil, not, or, repeat, return, then, true, until, and while. Variable names can only include letters, numbers, and underscores.
More with Variables
Lua is case-sensitive. This means that Hello, hello, HELLO, and HeLLo are all different things. Remember this. You could make a variable named While, just not while.
So you might be wondering when you're actually going to use variables. You'll probably be using them in functions and loops later, but they can be very handy in other places too. Say we have a brick in a model, and we want to change its properties.
brick = game.Workspace.Model.Model.Part
brick.Name = "My brick"
brick.Color = BrickColor.new("White")
brick.Position = Vector3.new(5,2,7)
brick.Size = Vector3.new(10,10,10)
Get the idea? You didn't have to type game.Workspace.Model.Model.Part every time you wanted to change a property. This can save a lot of time.
Another way to save time is by declaring variables like so: a, b, c = 1, 2, 3. In this example, a = 1, b = 2, and c = 3. See how that works?
Further reading: ROBLOX Wiki, Programming in Lua
Mini Lesson: Printing to the Output and Arguments
Ever wonder what the text is when you create a new script? There is a useful function in Lua called print(). It allows you to print the data in its arguments to the output. Put simply, the arguments are what is inside the parentheses. You can print any data type.
For example, print(2+2) would print 4 to the output. print 'Hello' prints 'Hello'.
You can even print variables!
var = "Hello world!" print(var) --> Hello world!
Multiple items can be printed in one statement, as long as commas seperate them:
print('Hello', 5+3, 'Bye!')
For example, print(2+2) would print 4 to the output. print 'Hello' prints 'Hello'.
You can even print variables!
var = "Hello world!" print(var) --> Hello world!
Multiple items can be printed in one statement, as long as commas seperate them:
print('Hello', 5+3, 'Bye!')
Global Variables
Normally, variables are only able to be used in the script they are created in. So what do you do if you want to use a variable in multiple scripts?
Simply put "_G." in front of its name everywhere you use it! For example:
_G.MyVar = "I'm a global variable!"
MyVar = "HAI"
print(_G.MyVar) --> I'm a global variable!
print(MyVar) --> HAI
In a different script:
print(_G.MyVar) --> I'm a global variable!
print(MyVar) --> nil
Since our "normal" variable wasn't defined in the second script, it is nil. Since our global variable is global, it works in every script in your game. You can modify global variables from any script, too!
Simply put "_G." in front of its name everywhere you use it! For example:
_G.MyVar = "I'm a global variable!"
MyVar = "HAI"
print(_G.MyVar) --> I'm a global variable!
print(MyVar) --> HAI
In a different script:
print(_G.MyVar) --> I'm a global variable!
print(MyVar) --> nil
Since our "normal" variable wasn't defined in the second script, it is nil. Since our global variable is global, it works in every script in your game. You can modify global variables from any script, too!
Variables Scopes
Variable scopes can be very confusing. When you make a variables with nothing in front of it (like _G) it can be accessed by the entire script that it is in, and nothing else. But you can make a local variable by putting local before a variable's name: local hi = "Hello there."
These local variables can be accessed only by the block they are in. This block is the variable's scope. In the picture to the left, each color is a different scope.
Image credit wiki.roblox.com. Visit this page for more information.
These local variables can be accessed only by the block they are in. This block is the variable's scope. In the picture to the left, each color is a different scope.
Image credit wiki.roblox.com. Visit this page for more information.
Data Types
The main data types in Lua are:
Boolean - True or False
Nil - Nonexistent, Nothingness
Number - A double-precision floating point (positive/negative, possibly a decimal)
String - A sequence of characters in quotation marks. Two can be combined (concatenated) using the ".." operator: print("I'm a string, " .. ' and I am too.')
Most methods and functions don't require strings to be in parentheses; print 'Hi'
is valid. For more great string functions, check out this wiki article.
Table - Used to store multiple values. You'll learn about this data type soon.
You can easily convert strings to numbers and vice versa, using tostring and tonumber.
print(tostring('I like the number ' .. 2*2 .. ', do you?'))
> I like the number 4, do you?
For more on tonumber, see here.
The following are ROBLOX specific data types:
BrickColor - A brick's color: BrickColor.new'White'
CFrame - A Coordinate Frame - The location and rotation of an object
UDim2 - A Gui's screen position: UDim2.new(xScale, xOffset, yScale, yOffset)
Vector3 - A brick's position or size: Vector3.new(x, y, z)
For a few more, see the nice Wiki link below.
Further reading: ROBLOX Wiki, Programming in Lua
Boolean - True or False
Nil - Nonexistent, Nothingness
Number - A double-precision floating point (positive/negative, possibly a decimal)
String - A sequence of characters in quotation marks. Two can be combined (concatenated) using the ".." operator: print("I'm a string, " .. ' and I am too.')
Most methods and functions don't require strings to be in parentheses; print 'Hi'
is valid. For more great string functions, check out this wiki article.
Table - Used to store multiple values. You'll learn about this data type soon.
You can easily convert strings to numbers and vice versa, using tostring and tonumber.
print(tostring('I like the number ' .. 2*2 .. ', do you?'))
> I like the number 4, do you?
For more on tonumber, see here.
The following are ROBLOX specific data types:
BrickColor - A brick's color: BrickColor.new'White'
CFrame - A Coordinate Frame - The location and rotation of an object
UDim2 - A Gui's screen position: UDim2.new(xScale, xOffset, yScale, yOffset)
Vector3 - A brick's position or size: Vector3.new(x, y, z)
For a few more, see the nice Wiki link below.
Further reading: ROBLOX Wiki, Programming in Lua