"

2 Introduction to Data Types

Numbers, Strings, and Booleans in Lua

Hasan Malik and Rutwa Engineer

Learning Objectives

  • Learn why we need data types
  • Learn about the following data types in Lua:
    • Boolean
    • Number
    • String
  • Learn how to use data types depending on the type of information needed

Introduction

This module explains the concept of data types in Lua.

In any computer program, you need to store information. For example, if you are writing a program for a medical database, you need to store the name, age, health status, upcoming appointments, etc of each patient. Similarly, if you are coding a game, you need to store the player’s username, the number of levels they have completed, their highscore, etc.

All of these pieces of data belong to a data type. In Lua, there are three fundamental data types that we will discuss—numbers, strings, and booleans.

What are Data Types?

In programming, we work with different kinds of information, like a player’s name (text), a player’s health (number),  or whether a door is locked (true or false, i.e. boolean). Each category of values is called a data type. Understanding these data types allow us to store and manipulate information inside our scripts!

Exploring Data Types Inside Roblox Studio

To change the properties of a part, as shown below, observe the Properties window:

The left column is a list of properties that we can modify, and the right column gives the values for those properties, each with different data types. For example:

  • BrickColor: takes in text to change the part’s colour — a string value
  • CastShadow: turns on and off the part’s shadow onto its environment — a boolean value
  • Transparency: takes in a number from 0 to 1 to change the part’s transparency – a number value

If every property was the same data type, we wouldn’t be able to control different aspects of a game. In the following section, we’ll explore each data type in detail and their applications inside Lua scripts.

The number Data Type

The number data type is used to store and work with numerical values.

The number data type doesn’t differentiate between whole numbers (integers) like 5 or -100, or decimal numbers like 3.14 or -0.5. These numbers can range from -1.7 x 10^308 to +1.7 x 10^308 (around 15 digits of precision, positive or negative)—enough numbers for us to work with!

Numbers can be printed using the print function.

Basic number operations

We can use number operations to perform calculations or compare values.

There are two main types of number operations in Lua: arithmetic operations and comparison operations. First, let’s discuss the six primary arithmetic operations: addition, subtraction, multiplication, division, exponents, and modulus.

Addition (+)

print(2 + 2) will output 4. Try it!

Subtraction (-)

print(5 - 3) will output 2

Multiplication (*)

print(4 * 3) will output 12

Division (/)

print(6 / 5) will output 1.2.

Note: the Division (/) operator will always output a decimal number, even if the result is an integer, like 1.0. So, print(6/6) will output 1.0 rather than 1.

(This is actually quite common in many programming languages, for example Python.)

Integer Division

A cool feature in Lua is the floor division function (or integer division operator).

If you type print(6 // 5) with two slashes, the quotient will automatically be rounded down to the nearest whole number, which in this case is 1.

Exponentials (^)

print(2 ^ 6) will output 64.0

print(2 ^ -1) will output 0.5

print(4 ^ 0) will output 1.0

print(49 ^ 0.5) will output 7.0

Note: Just like the division operator, the exponential operator will also always output a floating-point number (decimal number), even if the result is an integer.

Modulus (%)

The modulus operator returns the remainder of a division. For example,

print(10 % 3) will output 1. This is because 10 / 3 is equal to 3 remainder 1.

Likewise,  print(19 % 5) will output 4.

Arithmetic Hierarchy

In Lua, arithmetic operators follow a specific order of precedence, which determines the sequence in which parts of a mathematical expression are evaluated.

From highest to lowest precedence:

  1. Exponentiation (^)

  2. Multiplication (*), Division (/), Floor Division (//), Modulus (%)

  3. Addition (+), Subtraction (-)

Operators with the same precedence level are evaluated left to right.

For example, consider print(2 + 3 * 4 ^ 2 // 5 - 1). What do you think it will output?

Show/Hide

10

Exercise

All of the above operators are arithmetic operators. Soon, we will learn about another type of operator: the comparison operators.

The string Data Type

The string data type represents what we commonly refer to as “text”. For example, this whole paragraph is a string. Your favourite book is a string. Any email you send is a string. Your name, too, is a string.

Strings must be enclosed in quotes (just like we discussed in Print Statements!). These include both single-quotes (‘ ’) and double-quotes (“ ”). Either is fine—just remember to be consistent!

Examples of strings include:

  • “James”
  • 'Textbook'
  • “Hello World”
  • “My name is Takeshi. My age is 34. My job is software engineering. I studied at the University of Toronto.”
  • ‘nguyen.vinh@gmail.com’

Note: As shown above, strings can even include numbers. They can also include spaces and symbols, such as .!:;/>&%-+#@.

Exercises

 

Click here after solving the MCQ

The correct way to do this would’ve been to use single quotes to differentiate the internal quotes from the external quote. This will not cause any error: print("My mom told me to 'clean my room' yesterday.")

 

Exercise: Use the print command on a few strings of your choice. Try to print your name and some of your hobbies and activities!

Remember—to learn coding, practice is key, and the best practice comes by playing around with the tools as you’re learning them 🙂

The boolean Data Type

Another data type is boolean. The boolean data type can only have 2 possible values: true or false.

For example, the CastShadow property (discussed above) has a checkmark value which can be turned on (set true) or turned off (set false).

As we progress through the course, we’ll see how useful booleans can be in checking conditions, breaking out of conditional loops, and helping our code make decisions.

Note: Booleans are not strings. This is evident by the fact that they are not enclosed by quotation marks.

Exercise

Back to numbers: Comparison Operators

We use comparison operators to check if numbers are equal, greater than, or less than another number. This is useful for making decisions in our programs.

Common Mistake

Unlike arithmetic operations, which result in a number, comparison operators result in a boolean.

Let’s look at some comparison operators:

Equal to (==)

print(5 == 5) will output true

Not Equal to (~=)

print(5 ~= 5) will output false

print(5 ~= 3) will output true

Greater Than (>)

print(10 > 7) will output true

print(10 > 10) will output false

Less Than (<)

print(10 < 7) will output false

Greater Than or Equal (>=)

print(10 >= 10) will output true

print(10 >= 13) will output false

Less Than or Equal (<=)

print(10 <= 13) will output true

Play around with comparison operators to get comfortable with using them!

Congratulations—you now know about the three most fundamental data types in Lua!

 

License

Learn Coding and Animations in Lua Code Copyright © by Kyra Ma; Hasan Malik; and Rutwa Engineer. All Rights Reserved.