Lua Programming Language: Lua Coding Tutorial for Kids

Learn Lua Coding fast with this beginner’s guide.  

Introduction to Lua Programming

As a language for extending software applications, Lua was initially created in 1993 to meet the rising demand for customization at the time. It offers the fundamental programming features found in most procedural programming languages, but it does not include more complex or domain-specific features. Instead, it includes mechanisms for language extension that enable programmers to add these features.

Although the inventors of Lua worked on enhancing its speed, portability, extensibility, and ease of use in development to make it better, Lua programming is designed to be a broad embeddable extension language.

What is Lua?

Lua is an open-source language and it has its value across multiple platforms ranging from large server systems to small mobile applications.

Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes developed the extensible, lightweight Lua programming language as an internal project in 1993. It is implemented in C.

It was designed from the beginning to be software that could be integrated with the code written in C and also with other conventional languages.

Lua is cross-platform because its C API is reasonably straightforward and its interpreter for compiled bytecode is built in ANSI C.

This connection offers many advantages. Lua has a safe environment, automatic memory management, and a good facility for handling strings and other types of data with dynamic size because it aims to provide what C is not good at, such as a good distance from the hardware, dynamic structures, no redundancies, and ease of testing and debugging.

In Roblox, the coding language used is Lua, and it is the easiest coding language to learn. When we are used to Roblox Studio, the code runs fast.

Kids using Computer

Lua Programming Usage

In Game Development

As it is widely used by many game developers for popular games, some of them are mentioned below

  • Roblox
  • SimCity 4
  • Warframe
  • World of Warcraft
  • CRYENGINE

Used by Top Global Companies

Some of the companies that are using Lua are: 

  • Shopify
  • Thumbtack
  • Close
  • Unacademy
  • Kong
  • Tokopedia, etc.

Features of the Lua Language

Lua provides some of the unique features that make it distinct from any other language. Here are some of them mention below:

  • Extensible
  • Simple
  • Efficient
  • Portable
  • Free and open

    Application of the Lua Language

    Some of the real life application of Lua programming language are:

    • Game Programming
    • Scripting in Standalone Applications
    • Scripting on the Web
    • Extensions and also its add-ons for databases like MySQL Proxy and MySQL Work Bench
    • It has a Security system like Intrusion Detection System.

    Advantages of the Lua Language

    • It has very efficient memory usage.
    • It has an open-source license.
    • It is very easy to insert into the C programming language.
    • It is very good scripting for games.
    • It is very simple to use and to learn.
    • The syntax is pretty good and easy.
    • It is flexible so that it can be extended as needed.
    • It fits different problem types.
    • It is a first-class function.
    • It is a garbage collection.
    • It is a closure, proper tail calls.
    • It is coercion, it means automatic conversion between string and number values at run time. 
    • It is a coroutine, which means cooperative multitasking.
    • It is a dynamic module load.

    Few Drawbacks of the Lua Language

    • Some of its codes need to be coded manually.
    • It is a new language and it has a small community of users or clients.
    • It makes mistakes when we declare any variables.
    • It doesn’t support the general-purpose exception handling.

    Creating a Script in Roblox Studio

    Creating a script is very easy. Scripts can be inserted almost everywhere. Move over to the Model on the top panel and search to the far left. Click on the Script, which will generate a script that we can use for our coding. It is case-sensitive.

    Here are some of the concepts explained.

    Comments in the Lua Language

    A comment in Lua coding is the text that is completely ignored by the compiler or the parser. Comments are generally added by the scriptwriter or the programmer to explain or annotate the code that is written. They are added to make the source code easier to read and understand for humans.

    To make a short comment, we have to start with a double hyphen (–) anywhere outside a string and it extends to the end of the line in code.

    For Example:

    — This is a comment

    local var = 44 — This is a comment for the following code on the same line

    Comments for multi-line or several lines can also be defined using a long format that is enclosed by the brackets. 

    For Example:

    –[[

    This is a multi-line comment.

    And it can also contain short comment hyphens

    ]]

    Print Function in Lua Coding

    Print is called the heart and soul of coding. Print is a built-in function that helps us to print out whatever statement we want to display on the console. It can be either numbers or strings and we can see that once we launch a script, there is already a print function automatically typed and present in it. We will erase it to continue with the next step.

    Type the word print in all lowercase letters. In some cases, if the print is typed incorrectly or typed with any capital letter, the program will not recognize the command and will not run it because it is case sensitive.

    If you add two round parentheses, the studio should automatically add the second one when you finish adding the first. However, there must be no space between the print and the parenthesis for the Lua to be able to read and understand it.

    For example:

    print()

    Strings in Lua Programming

    Strings are used to hold the text of our code. They are bracketed or covered by apostrophes or quotation marks. in the print () function, we need to add a string in-between the parenthesis so that we don’t just print space.

    We can also use a pair of single quotes (’’) or a double parenthesis (“ ”). A string is nothing but any character or words inside of a pair of quotes, such as 

    print(“BrightChamps”)

    we can also replace it with any other text that we want it is in-between the parenthesis (“).

    Expected output:

    BrightChamps

    Variables in Lua

    A variable is a container or memory region with a name and the ability to store a value. Any data type, including numbers, texts, Booleans, and many others, can be used as a variable’s value.

    Naming Variables in Lua

    The rules in Lua variable naming are is mentioned below

      • The variable name can be of any non-reserved string of letters
      • The variable name can have a digits
      • The variable name cannot start with a digits
      • The variable name cannot have a space in between
      • The variable name cannot be a keyword
      • The variable names are case sensitive 
      • The variable name can have underscores as a special character

    Examples 

    LETTEr       — valid

    a12        — valid

    var_1name  — valid

    _2test     — valid

    Var12name  — valid

    else     — NOT valid

    5th       — NOT valid

    As Lua programming language is case-sensitive the variable name TestVar and TESTVAR are two unique names. 

    NOTE: The names that begin with an underscore followed by the uppercase letters should be avoided, as they may be reserved for internal global Lua variables.

    For Example:

    _VERSIONs

    Some of the Reserved Names in Lua are as follows and cannot be used as variable or function names: and, for, or, break, function, repeat, do, if, return, else, in, then, else if, local, true, end, nil, until, false, not, while.

    Variable Assignment

    Assigning a value to a variable is done with the help of the assignment operator that is = the operator. The variable is always on the left-hand side of the = operator and while the value is always on the right-hand side.

    x = 4

     

    word = “Hi”

     

    reference = workspace.Part

     

    print(x)

     

    print(word)

     

    print(reference)

    Expected output:

    4

    Hi

    Part

    Once we have declared a variable value later it can be changed by simply assigning another value to it as shown below:

    x = 550

    print(x)

    x = 5542

    print(x)

    Expected output:

    550

    5542

    Lua also allows us to set or change multiple variables in the same command just by separating each variable-value pair with a comma as shown below:

    a, b, c = 4,5,6

    print(a)

    print(b)

    print(c)

    Expected output:

    4

    5

    6

      Variable Scope

    In Lua, variables exist in two possible scopes, that is a global and a local variable scope. All variables with the default scope will be global unless it is declared as local.

    • Global Variable Scope

    A global variable is visible to all the scopes of a script or the coding. 

    x = 1 — Global variable “x”

    for i = 1, 5 do

    x = x + 1

    print(“Global ‘x’ = ” .. x)

    end

    print(“Global ‘x’ = ” .. x)

    Expected output:

    Global ‘x’ = 2

    Global ‘x’ = 3

    Global ‘x’ = 4

    Global ‘x’ = 5

    Global ‘x’ = 6

    • Local Variable Scope

    Only the block where it was declared can access or use a local variable. Because they are incorporated into the same environment in which they were formed, local variables are quicker than global variables.

    We should always use local variables over global variables unless we don’t have a specific reason. Local variables are declared using the local keyword as shown below:

    local x = 110  — Local variable “x”

    For example:

    local x = 445  — Local variable “x”

    for i = 1, 5 do

    local x = 5  — Different variable “x”, local to this “for” loop

    print(“Loop ‘x’ = ” .. x)

    end

    print(“Initial ‘x’ = ” .. x)

    Expected output:

    Loop ‘x’ = 5

    Loop ‘x’ = 5

    Loop ‘x’ =5

    Loop ‘x’ = 5

    Loop ‘x’ = 5

    Initial ‘x’ = 445

    Operators in Lua Programming Language

    The Print function also works for numbers and equations, then it will automatically simplify the output. But before we write our program, we have to find the symbols that need to get the right answer.

    So here are Arithmetic Operators.

    Arithmetic operators are all the tools that we use in our program to indicate a relationship between numbers. These are what we call the symbols that we use to add, subtract, multiply, and also divide the numbers.

    Some of the Arithmetic Operators that we used in Lua are:

    • Addition: +
    • Subtraction: –
    • Multiplication: *
    • Division: /
    • Exponents: ^
    • Modulus: % (used to find the reminder)

    In print, we will write our equation in-between the round parenthesis. 

    For example:

    print(5 + 5 * 11^2 / 10 – 20)

    Lua uses the math rules for the Order of Operations. It means, it starts with exponents, division, multiplication, and finally addition and subtraction.

    For example,

    print(2 + 5 * 3)

    by looking at the above equation we say the answer is 21, since you add 2 + 5, get 7 and, multiply by 3, this is wrong. Instead of that, we have to multiply 5 and 3 first, then add 2, getting 21. So, this is a rule of both math and programming.

      Tables in Lua Language

      Tables are used for storing a very large set of data. A table is a Lua that includes the data type to store values like numbers, Booleans, strings, functions, and many more. We can access and manipulate the data easily.

      These are more lists that contain the indexes of objects, arrays, and more. To indicate the tables, we use the curly braces {} as shown here:

      local p = {}

      print(p)

      Expected Output:

      table: 0035AE18

      Once the table is been created it behaves as either an array or a dictionary. This concept we are going to learn is below.

      Lua Arrays

      Arrays are simply a list of order values that are compiled in a table. This can include strings, numbers, Booleans, functions, and so on.

      To make an array, we have to start by assigning a name to it of our own choice. it is useful for storing collections of data such as a group of players with special permissions.

      • Creating Arrays

      To successively store the data, with commas separating them, in a Lua table to build an array. Any non-nil type, including Boolean, number, string, function, user data, or another table, can be a value for an array.

      To assign a table to the data by adding an “=” in between, and write a pair of curly brackets.

      For Example:

      local randArray = {}

      Then we place all of the values inside the array. We can add more variety to our table but we have to make sure to separate each value by adding commas in-between them.

      For Example:

      local randArray = {“Brightchamps”, 10, “welcome to the brightchamps”, 17}

      Lua scripting uses 1-based indexing for arrays so each value in the array has an index or number that is assigned to it. For example, the first value is 1, the second value is 2, the third value is 3, and so on. 

      • Reading an element from Arrays

      To print a specific part of the array, we have to write the table name in the print function and then the index of the value, the in-between square brackets [].

      For Example:

      local randArray = {“Brightchamps”, 10.1, “welcome to the brightchamps”, 17}

      print(randArray[1])

      the output will be the first string:

      Brightchamps”.

      • Writing Into Arrays

      The value of an array and its index can be redefined or rewritten by making use of the index number in the brackets ([pos]) that is followed by the = operator and then specifying the value.

      For Example:

      local randArray = {“Brightchamps”, 10.1, “welcome to the brightchamps”, 17}

      randArray [2] = 12345

      randArray [4] = “New string”

      print(randArray [2])

      print(randArray [4])

      Expected Output:

      12345

      New string

      Inserting Items at the end of an array using the methods or the function so the LUA uses the table.insert() function to add elements or values into the array.

      To Add the new item into the array using this syntax t[#t+1].

      For Example:

      local randArray = {“Brightchamps”, 10.1, “welcome to the brightchamps”, 17}

      table.insert(randArray, “New string”)

      randArray [#randArray +1] = “Another new string” 

      print(randArray [5])

      print(randArray [6])

      Expected Output:

      New string

      Another new string

      If we want to insert an item in between the start and the end by specifying the position value as a second argument of the table.insert() function.

      So this will insert the new item and push the item that is present with one index position ahead.

      For Example:

      local randArray = {“Brightchamps”, 10.1, “welcome to the brightchamps”, 17}

      table.insert(randArray, 2, “NEW ITEM #2”)

      print(randArray [1])

      print(randArray [2])

      print(randArray [3])

      Expected Output:

      Brightchamps

      NEW ITEM #2

      10.1

      • Iterating Over an Arrays

      Arrays can be iterated over or looped through in the following two ways:

        • By making use of the built-in ipairs() function for a loop.

      For Example:

      local TArray = {“this is a string”, 3.9, workspace.part, “New string here”}

      for i, value in ipairs(TArray) do

      print(i, value)

      end

      Expected Output:

      this is a string

      3.9

      workspace.part

      New string here

        • By Getting the array length using the # operator and looping it from 1 to that length of the array.

      For Example:

      for ii = 1, #testArray do

      print(ii, TArray[index])

      end

      Expected Output:

      this is a string

      3.9

      workspace.part

      New string here

      Removing Items from Arrays in Lua

      If we want to remove an item from the array so the LUA provides a function called a table.remove().

      This function will help us to remove the item at the specified position and move an item down one index position.

      For Example:

      local randArray = {“Brightchamps”, 10.1, “welcome to the brightchamps”, 17}

      table.remove(randArray, 2)

      print(randArray [1])

      print(randArray [2])

      Expected Output:

      Brightchamps

      Welcome to the brightchamps

      • Dictionaries

      Dictionaries are the extension of an array. As we know the array stores an ordered list of items, and a dictionary stores a value set of key-value pairs.

      Key Value

      FruitName mango 

      FruitColor yellow

      Sour false

      • How to Create Dictionaries

      To create a dictionary, we have to keep remember that each key is followed by = and then the value we have to write, and each key-value pair is separated with a comma.

      For Example:

      local testD = {

      FruitName = “mango”,

      FruitColor = “yellow”,

      Sour = false

      }

      The keys are not limited to string names but a key also may be an Instance, so the key must be declared by surrounded by the brackets ([key])

      For Example:

      local p = Instance.new(“Part”)

      local testD = {

      PType = “Block”,

      [p] = true

      }

      • Reading From the Dictionaries

      To read it from a dictionary, add a pair of brackets after it specifies the reference and then specifies the key name.

      For Example:

      local p = Instance.new(“Part”)

      local testD = {

      PType = “Block”,

      [p] = true

      }

      print(testD[“PType”]) 

      print(testD[p])  

      Expected Output:

      Block

      true

      The String keys like PType must be surrounded with the quotes as in testD[“PType”]. Non-string keys like [p] should not be surrounded by quotes.

      • Writing Into the Dictionaries

      The value of a new or the existing dictionary key can be defined by indicating the key name in the square brackets [key] that should be followed by = and then the value in it.

      For Example:

      local testD = {

      FruitName = “mango”,

      Sour = false

      }

      testD [“FruitName”] = “Banana”

      testD [“Sour”] = false

      testD [“FruitCount”] = 20

      print(testD [“FruitName”])

      print(testD [“Sour”])

      print(testD [“FruitCount”])

      Expected Output:

      Banana

      False

      20

      • Iterating Over the Dictionaries

      Dictionaries can be iterated or read over with the help of the built-in pairs() function in a for a loop.

      For Example:

      local testD = {

      FruitName = “mango”,

      FruitColor = “yellow”,

      Sour = false 

      }

      for key, values in pairs(testD) do

      print(key, values)

      end

      Expected Output:

      FruitNamemango

      FruitColoryellow

      Sourfalse

      Note: using the ipairs() function in the array, dictionary iteration happens via the pairs() does not necessarily return items in the same order as it is mentioned in the dictionary.

      • Removing Key-Value Pairs

      To remove a key-value pair from the dictionary, we have to set its value to nil. The following example completely erases the key-value pair from the dictionary.

      For Example:

      local testD = {

      FruitName = “mango”,

      FruitColor = “yellow”,

      Sour = false 

      }

      testD [“Sour”] = nil

      for key, value in pairs(testD) do

      print(key, value)

      end

      Expected Output:

      FruitName mango

      FruitColor yellow

      • Tables as References

      If we have stored a table in a new variable, at that time copy of that table is not created but the variable becomes a reference that is a pointer to the original table or a variable.

      It means any changes to the original table will be reflected in the references.

      For Example:

      local originalA = {10, 20}

      local arrayR = originalArray

      print(“Original values:”, originalA[1], originalA[2])

      print(“Reference values:”, arrayR[1], arrayR[2])

      originalA[1] = 1000

      originalA[2] = 2000

      print(“Reference values:”, arrayR [1], arrayR[2])

      Expected Output:

      Original values: 10 20

      Reference values: 10 20

      Reference values: 1000 2000

      Concatenation in Lua Scripting

      To combine two or more separate values, we use concatenation in the print function. We can use the same format as adding two numbers but we have to add two periods or the dots after each string.

      We should not place the dot inside the string or else the Lua will think it is a part of the string and it sometimes produces an error.

      print(“It is around”.. ” 3: 44 PM”)

      The output will show us a full statement as if it is a single string, meaning the concatenation is successful.

      So, the output will look like something that is shown below:

      It is around 3: 44 PM

      Loops in Lua Programming

      Loops are the statements that allow us to execute the code or repeat code multiple times. In Lua coding, each loop repeats a block of code but in different ways.

      While Loop in Lua Script

      While loops repeat the sequences of code while the condition is true. If the condition is false, it will not execute the block of code and it ends. If the condition is true, the code between the do and end executes and the true/false condition is checked again.

      The while loop can also be a countless or infinite loop. The condition evaluates to be true or false that is Boolean value.

      For Example:

      local x = 1

      while x < 5 do

           print(“The value is less than 5”)

           x = x + 1

      end

      print(“The value is not less than 5”)

      Expected Output:

      The value is less than 5

      The value is less than 5

      The value is less than 5

      The value is less than 5

      The while loop is an infinite loop by using a true as the condition:

      while true do

      print(“Looping in the process…”)

      wait(0.5)

      end

      A wait() function is used to delay inside an infinite loop

      For Loop in Lua

      The for loop executes a command or collection of commands a predetermined number of times. A control variable, a start value, an end value, and an optional increment value will all be present in it. Up until the provided end value is reached, the loop will keep repeating.

      The starting value increases by 1 by default, we can also change it by adding a comma after the end value.

      The Syntax:

      for (VariableName) = (Start Value), (End Value), (Number of values increases by optional)

          (Statements)

      End

      For Example:

      for count = 1, 5, 2 do

      print(count)

      end

      Expected Output:

      1

      3

      5

      Repeat/Until Loop in Lua

      A repeat loop repeats until a certain condition is met or done. The code between repeat and until is executed at least once because the conditional test is performed afterward as it is a post-conditional loop.

      For Example:

      local x = 1

      repeat

          print(“The value is less than 5”)

          x = x + 1

      until(x > 5)

      print(“The value is greater or equal to 5”)

      Expected Output:

      The value is less than 5

      The value is less than 5

      The value is less than 5

      The value is less than 5

      The value is greater or equal to 5

      Break

      If we are having a loop that is infinite while true do loop then we can force it to end with the help of the break command. If we are writing break at the end of a loop then it will end the loop if a condition is true.

      We can also use this in a nested loop. A nested loop is a loop with another loop inside of it.

      For Example:

      local a = 0

      local timout = 5

      while true do

      print(“Looping….”)

      wait(0.1)

      a = a + 1

      if a == timout then

      break

      end

      end

      print(“Loop ended so moving on!”)

      Expected Output:

      Looping….

      Looping….

      Looping….

      Looping….

      Looping….

      Looping….

      Loop ended so moving on!

      Functions in Lua

      When we are writing a sequence of code that needs to be repeated in a different place, it may be a little difficult to write it several times. Functions in Lua are a set of instructions that can be used multiple times in a script and they can be repeated and manipulated a lot easier. A function can be called or executed through a command or triggered through an event.

      There are built-in functions of Lua, such as print.

      • Creating/ Defining a Functions

      A basic function declaration includes the function keyword and then assigns it a name by following the rules then adding a pair of parentheses () after the function name without any spacing in-between, and then pressing enter from the keyboard.

      The function body will be a block of code, so it must be closed with the end keyword or close off your function or else Lua will give us an error.

      For Example:

      local function addNum()

      end

      The commands will be executed which is inside the block when the function is called.

      For Example:

      local function addNum ()

      — this is a Function body

      print(“inside the Function when it is called!”)

      end

      Now we have our function but still, we don’t get any output because we have not called our function yet to execute. 

      • Calling a Function

      Once a function is defined or created then it should be executed by calling it but the functions do not self-execute so.

      To execute or call a function we need to type the function name on the next line, and Lua will print the strings that are in the function.

      For Example:

      local function addNum ()

      — this is a Function body

      print(“inside the Function when it is called!”)

      end

      addNum ()

      Expected Output:

      inside the Function when it is called!

      Note: if we are using the variables have to be defined before the functions get executed or else it will give us nil. 

      • Local Variables and Code Blocks

      Local variable scope is much more limited as explained above but the global variables can be accessed anywhere in our script or the code after being defined, whereas the local variables are limited to a block.

      What is a code block? The sequence of code that is inside our function.

      For Example:

      local function addNum ()

      — this is a Function body

      local myVar =”welcome to the brightchamps”

      print(myVar) 

      print(“inside the Function when it is called!”) 

      end

      addNum ()

      Expected Output:

      welcome to the brightchamps

      inside the Function when it is called!

      Note: If we try to print the local variable outside the function block, we are going to get the nil since the variable can’t be accessed outside.

      • Function Arguments and Parameters

      Whenever we execute a function, we might want to change certain parts of the code every time. So, the Parameters are used to tell Lua which variables inside the function we want to change. 

      Here we can manipulate the variables of the functions with ease. The pair of parentheses we have when creating a function or when we are calling a function that we used to put arguments and parameters.

      Functions may allow us to send in data as parameters. We can place one or more parameter names inside parentheses after declaring a function.

      For Example:

      local function addNum (n1, n2)

      end 

      When we are calling a function with parameters, we should specify that the values should be passed to the function. An argument is a value that we assign the code to it when it is executing. It comes inside our next pair of parentheses and then we can be changed it every time we execute the function separately as shown below. 

      Note: Arguments and the parameters are not limited to a single pair we can have as many as we want but make sure each argument and parameter are separated by a comma.

      For Example:

      local function addNum (n1, n2)

      local result = n1 + n2

      print(“So the result is”..n1 .. ” + ” .. n2 .. ” = ” .. result)

      end

      addNum(7, 3)

      addNum(5, 0.5)

      addNum(100, 5000)

      Expected Output:

      So the result is 7 + 3 = 10

      So the result is 5 + 0.5 = 5.5

      So the result is 100 + 5000 = 5100

      Note:  The arguments and the parameters should be in the same order or else it will assign the wrong value to the variable. 

      Function with the parameters is always local to the function and it is used only inside the function’s scope and also its descending scopes.

      Note: In the Lua function, if we are passing more parameters than it’s expecting, the excess ones will be ignored and if the function expects more parameters than we have provided, the value nil will be passed or used for all missing parameters. 

      • Returning a Value

      Here the functions can also return or send back the data to the calling function or command by making use of the return keyword.

      For Example:

      local function addNum(n1, n2)

      local r = n1 + n2

      return r

      end

      Note: we should not put any commands after a return command or else it will give us an error.

      When a function returns then it should be assigned to a variable or used it.

      For Example:

      local function addNum(n1, n2)

      local r = n1 + n2

      return r

      end

      — a function calling

      local result1 = addNum (2, 3)

      print(result1)

      local result2 = addNum(1000, 1000)

      print(result2)

      Expected Output:

      5

      2000

      Here the Lua even lets us return multiple values from a function:

      For Example:

      local function addNum(n1, n2)

      local sum = n1 + n2

      local diff = n1 – n2

      return sum, diff

      end

      local sum, difference = addNum (2, 3)

      print(sum)

      print(difference)

      Expected Output:

      5

      -1

      Properties in Lua

      If we want to modify any workspace object’s settings, such as transparency or invisibility, we must lay down the specifics so that Lua is aware of our intentions.

      Sometimes if we want to change the transparency of any part or object, we need to define that the part is inside the game.

      Workspace, with the game that we are working on and workspace being the place where the part is and the rest of our parts are stored. Then, we have to write the object or a part name that we want to change (part) and the setting or the property that we want to change like its transparency, and then assign a value that we wish to.

      If we want it to be fully invisible then set the value to 1, 0 will be visible.

      game.WorkSpace.part.Transparency = 1

      If we run the code or play the game, the output will be the portion that is entirely invisible.

      Booleans in Lua

      Only two values make up the Boolean. Booleans are based on conditions that either give you true or false in scripting. A Boolean value will be generated when the values are compared.

      Relational Operators

      The Relational operators are the operators that we used to compare values with one another values.

      The following are some of the relational operators:

      • Equal to this is used to compare two values: ==
      • Not equal to, it gives us true if the values are not equal: ~=
      • Greater than: >
      • Less than: <
      • Greater than or equal to >=
      • Less than or equal to <=

      For Example:

      5 > 4

      12 == 12

      8 < 3

      Output:

      True

      True

      False

      Note: Never get confused with the equal =, this is used to assign something and == is used to compare two values.

      If Statements in Lua

      If the statement is a decision-making statement that makes sure some specific commands only happen if the condition is true then it will execute the code.

      For Example

      if 110> 22 then

      print(“Welcome to the Brightchamps”)

      end

      Expected Output:

      Welcome to the Brightchamps

      Another way to say true instead of the above method so that it prints the string.

      if true then

      print(“Welcome to the Brightchamps “)

      end

      Expected Output:

      Welcome to the Brightchamps

      Lua Local Scripts

      There is something called a local script in Roblox studio. Local scripts are scripts it affects the user’s client rather than the server.

      We only view a user’s client, not what the entire server can see. Our friend may be playing the same game as us, yet we may be seeing something entirely different from him.

      It means that the scripts that affect the server are seen by everyone and not just the individual’s client.

      Lua Programming FAQs

      What is the Scripting Language Roblox Developers Use?

      Roblox uses Lua as the main language.

      Is the Roblox Lua and Lua is same?

      No, they are not the same.

      What is the difference between the Roblox Lua and Lua?

      Roblox developing language Lua is similar to Lua and only it has a few minor differences. Roblox Lua is a unique scripting language that utilizes the Lua programming language.

      Is Lua Easy to Learn?

      Yes, it is easy to learn when Compared to some other coding languages for beginners.

      Book Free BrightChamps Class Widget

      Get a Talent Discovery Certificate after trial class

      100% Risk-Free. No Credit Card Required