The basics

Now, let's dive into the syntax of Luau. The syntax is how the code is structured. Let's first learn the data types. We often use data types when coding. It is the type of data a variable has, such as a text or a number.

Data types

The most common data types, across most programming langauges, are:

  • String (text)
  • Integer (whole number)
  • Float (decimal number)
  • Boolean (true or false)

The string data type

A "string" refers to a piece of text. You can make one in Luau by enclosing text in between double quotes ("Example") or single quotes ('Example').

We can assign it to a variable, use it, modify it, etc. You'll learn more about variables in the next section after Data types, "Variables".

local a = "Hello!"
print(a) -- Output: Hello!

a = "Goodbye!"
print(a) -- Output: Goodbye!

The integer data type

If you've been in math class, we should know that an integer refers to a whole number, a non-fractional number. They are values like 5, -8, 27, etc.

They're simple to work with. You can assign them to a variable directly, or increment/decrement them.

local x = 2
print(x) -- Output: 2

-- We can change it...
x = 5
print(x) -- Output: 5

-- ...or add/subtract them by a value (as well as other math operations)
x += 1 -- same thing as x = x + 1
print(x) -- Output: 6
x -= 3 -- same thing as x = x - 3 (You'll learn more about this later)
print(x) -- Output: 3

However, integers have a limit, which you need to be aware of. Since Roblox uses 64-bit integers, the highest allowed value is 2^63-1 or around 9.2 quintillion (9.2^18). If you attempt to store larger numbers, you will encounter something called an integer overflow. The lowest allowed value is -2^63 or about -9.2 quintillion. Practically, however, working with integers larger than 2^53 (9.0^15) will cause loss of precision since Luau uses double-precision floating-point to store numbers.

The float data type

A float is a number that can be fractional (with decimals). Another data type, called "double", is basically the same thing, except it has higher precision. float can go up to 4 bytes (32 bits), while double can go up to 8 bytes (64 bits).

We don't need to know these differences, as Roblox often refers to integers as integers, and floats and doubles as numbers.

The boolean data type

A "boolean" refers to a state which can only either be true or false. This is useful when we only want a value to either be YES / NO, ON / OFF, or TRUE / FALSE. Let's take a look at this example:

local DinnerIsReady = false

if DinnerIsReady == false then -- You will learn about if statements, loops (while do, for do), etc. later!
  print("Dinner is not ready.")
else
  print("Dinner is ready!")
end

Variables

Usually, when coding, we would want to store a data type for use of it later.