Lua: Variables and User Input (io.read)
In this tutorial we are going to learn about variables, and getting input from the user.
Variables
So firstly - variables. A variable is basically just something that can change. I like to think of a variable like a box – we can put something in the box, see what's in the box and change what's in the box.The creation of a variable is called the declaration (like creating a box and giving it a name) and the first assignment of that variable is called the initialisation (like putting something in the box for the first time).
We can declare and initialise variables in Lua very, very easily. You simply type the name that you want your variable to have (this needs to be something unique because we need to use the variable name every time we do something with it), followed by an equals sign, followed by the value you want to first set it to.
For example, the following creates a variable called 'variable' and set's it's value to the string of "value":
variable = "value"
Notice that as we discussed in the previous tutorial – we use double quotes to surround a string variable (a bunch of letters/characters).
It's worth noting that you can actually detect the data-type of a variable by passing it into the 'type' function. Doing this gives us a string of the data-type, so we can put the 'type' function call into the 'print' function to see what type a variable is.
For example, the following would output "string" because 'variable' is a string:
variable = "value" print(type(variable))
If we ever want to change the variable's value, we can simply do something to it via it's name. So if you most basically, just want to set the variable to something completely different – you can write the variables name, followed by an equals sign, followed by the value you want to set it to.
Before I show you the code snippet – let's very briefly talk about commenting. Commenting code is basically just leaving little notes in the code (usually describing functionality or what will be implemented next) that don't actually affect the execution of the code. You can leave single line comments in Lua by putting a double dash ('--') and then writing whatever you want for the rest of the line (the rest of the line will not affect the code's execution). For example:
print("Printing this text") --This is a comment, it's not executed
But back to where we were. A nice example of changing variables and using the 'type' function, would be to change the variable to various values and check the type of them (note that I've used the string concatenation operator that we discussed in the last tutorial to prefix some text before each type output):
variable = "value" --Declare and initialise 'variable'
print("Double quote surrounded text: " .. type(variable))
variable = 5 --Set 'variable' to 5
print("Constant number: " .. type(variable))
variable = 10 + "100" --Set 'variable' to the number 10 plus the string "100" (Note that Lua converted the string to a number for addition, for us!)
print("Number + String" .. type(variable))
On running this, everything should be as expected.
So now we can store pretty much any values we want in variables, let's learn how to get the user's input and put it into a variable!
User Input
So in the previous tutorial we used 'io.write' to write things to the screen – luckily for us there is another cool 'io.' function, and this one takes input from the user!It's named 'io.read'. It doesn't take any parameters and it's extremely easy to use!
Wherever you put the 'io.read' function, the value that the user enters will take it's place. So if you want to put whatever the user enters in a variable, you simply set a variable to the 'io.read' function!
For example:
userinput = io.read()
We'd probably want to also prompt the user for a value and will also probably want to do something with the user's input. For example, we could output the user's name back to them with a greeting (using the string concatenation operator)!
io.write("Enter your name: ")
name = io.read()
print("Hello, " .. name);
On execution, this should do as we expected!
You can (theoretically) have as many 'io.read's as you like in a Lua file, so we could even ask the user for more information!
io.write("Enter your name: ")
name = io.read()
io.write("Enter your age: ")
age = io.read()
print("Hello, " .. name)
print("You are " .. age .. " years old!")
print("In ten years, you will be " .. age+10 .. " years old!")
Back to Lua

