This tutorial is a simple lesson on the different types of variables that you are able to use. From basic ones such as strings and numbers to variables that can represent nothing.
We will start off with one of the most basic concepts, nothing. Nil represents, well, nothing. This value can be obtained by trying to refer to a variable that does not exist, or just by setting a variable itself to nil.
print(hello) -- Expected result: nil (This is because the variable does not exist)
test = nil
print(test) -- Expected result: nil
A string is a line of text that is surrounded by either double quotations (") or single quotations ('). When using either of those, you must remember to use whatever you chose to start for closing the string. There are cases when one might need to use a double quote inside a string, so don't forget that either of them work
text = "Hello!"
print(text) -- Expected result: Hello!
text2 = 'He said "Hello"'
print(text2) -- Expected result: He said "Hello"
Numbers are exactly what they are called, numbers. Compared to most of the other programming languages, Lua does not differentiate between whole numbers (commonly called integers) or decimals (also known as a float, or a floating point number).
num = 1
print(num + num) -- Expected result: 2
This isn't really something you see quite often, but you may sometimes run into problems with mathematical calculations. For example, if you end up with dividing 0 by 0 (0/0), you get nan, which stands for not a number. This may screw up your script and you need to take measures in case you deal with numbers that change all the time.
num = 0/0
print(num) -- Expected result: nan
A boolean is a variable that can represent only 2 states, true or false. Lua identifies false and nil as false, while everything else can be considered true when using conditions, even 0 and empty strings, contrary to other popular languages.
test = false
print(test) -- Expected result: false
test = 1
print(test) -- Expected result: 1
if test then
print(test) -- Expected result: 1
end
Tables are arrays that store information in a key/pair manner, meaning that for every key that is inside the table, there is a pair, or a value, that is attached to it. You can think of a table as a list of variables, where you can index and find the item that you need anytime you want, so long as you keep it organized.
t = {}
t["foo"] = "bar"
t[7] = 8
print(t["foo"]) -- Expected result: bar
print(t[7]) -- Expected result: 8
print(t.foo) -- Expected result: bar
print(t.7) -- Expected result: nil
--(You can't use numbers for indexing when using a period)
To remove something from a table, you can set the index to nil. Also, avoid using anything that creates NaN in your table, since it messes some things up.
Arrays: Arrays are tables that are ordered in numerical order, usually in order by entry time. This means that the earliest entry will be the first key in your table, which starts at 1. You can manipulate arrays by using table.insert(tbl, position, value) and table.remove(tbl,position). Do remember that not providing the position, such as doing table.insert(tbl, 5) will cause the value to be inserted or removed to/from the last position.
local tbl = {9,3,1,5}
print(tbl[1]) -- Expected result: 9
table.insert(tbl,10) -- Adds a fifth value to the table
print(tbl[5]) -- Expected result: 10
local removed = table.remove(tbl, 1) -- Removes the first value from the table and returns it
print(removed) -- Expected result: 9
Dictionaries: Dictionaries are a bit simpler than arrays, where you make indices (plural of index) based off of strings/text. You can add or remove from these as you please like so:
local tbl = {
["Hi"] = "Hello",
["What's"] = "Up",
["Pop"] = 5
}
print(tbl["Stuff"]) -- Expected result: nil
tbl["Stuff"] = "Things"
print(tbl["Stuff"]) -- Expected result: Things
print(tbl["Hi"]) -- Expected result: Hello
tbl["Hi"] = nil
print(tbl["Hi"]) -- Expected result: nil
It's always a good idea to keep arrays and dictionaries separate, but you can put these two concepts into one table:
local tbl = {1,5,6,["test"] = 12}
print(tbl["test"]) -- Expected result: 12
table.insert(tbl,7)
print(tbl[4]) -- Expected result: 7
Functions are blocks of code that are set up in such a way so as to remove the need to repeat it multiple times. The user will only need to call the name of the function along with the parameters needed to run the block of code.
function bap()
print("bop")
end
function beep()
print("boop")
end
bop = function()
print("bap")
boop = function()
print("beep")
end
bap() -- Expected result: bop
beep() -- Expected result: boop
bop() -- Expected result: bap
boop() -- Expected result: beep
Roblox has implemented their own way of identifying what your variable refers to, whether it be a roblox instance or some function.
num = 1
txt = "Hi!"
nl = nil
tbl = {}
print(typeof(num)) -- Expected result: number
print(typeof(txt)) -- Expected result: string
print(typeof(nl)) -- Expected result: nil
print(typeof(tbl)) -- Expected result: table
-- typeof() is a Roblox extension of Lua's default type()
The variables I have listed above are just the basic lua variables and I did not go too in-depth with my explanations. This was only made for those that are relatively new to Lua and are looking for a quick explanation. If you want more information on variables, I'd suggest going to the official Lua website or the Roblox developer page.
As a final note: If you need something clarified or if you have something to add on, just hop on the lua learning discord and ping me @jakehead20