Getting Started

Learn LuaU - By Luitrixx

If you need help Join the Support Server via the Menu


I'm working on more Documentations, I'll be soon releasing Documentations about:

OOP [Object Oriented Programming]
Typed LuaU

Join the support server via Menu to be notified!


Intro

Luau is a fast, small, safe, gradually typed embeddable scripting language derived from Lua. It is used by Roblox game developers to write game code, as well as by Roblox engineers to implement large parts of the user-facing application code as well as portions of the editor (Roblox Studio) as plugins.

Example:Declare Variables

  • Declare variables on LuaU is similar to Lua.
  • Note:[Variables can be named anything]
        
local myVariable = "Hey" --This is now our variable holding the value of "Hey"
print(myVariable) -- This will print out "Hey"
        
    

Example: Working with Numbers

  • When dealing with numbers [integers] You can do arithmetic.
  • You're provided with:[+,-,/,*,%] (Math Operators) for your mathematical operations.
        
local myNumberVariable = 5 --This is now our variable holding the value of "4"
local mySecondNumber = 30
print(myVariable) -- This will print out 5
print(myVariable + 4) -- This will print out 9
print(myVariable * mySecondNumber) -- This will print out 150
        
    
Introduction

What is Roblox LuaU?

Roblox Luau is a fast, small, safe, gradually typed embeddable scripting language derived from Lua. It is used by Roblox game developers to write game code, as well as by Roblox engineers to implement large parts of the user-facing application code as well as portions of the editor (Roblox Studio) as plugins.

Why LuaU?

Roblox LuaU is the primary coding language on roblox for game development. This also includes Inferfaces as well.

Good to know?

Though Roblox LuaU is devired from Lua, this does not mean that every single method, and features on Lua are on Roblox LuaU. Roblox has ended up entirely getting rid of some features that are available on Lua and this is mostly for security concerns.

LuaU Syntax.

LuaU is currently using the baseline syntax for Lua 5.1.
        
print("Hello World!") -- This will print out "Hello World!"
        
    
Output
The roblox output window, is a very essential tool when coming to coding on roblox. The output window allows us to see any printed statements, error and warnings. This way it will be easy for us to locate where,when and why an error/warning happened. Below I'll be showing you how to enable this window.

Head over to your view Tab.

Output

Now once you click on the view tab, Go ahead and open your output by clicking on output.

Output
Once you have done the steps above you should now have a Window Open. This window should look similar to the one displayed below.
Output
First Script
Since you now have your Output Opned,Let's get to see it work. On this section we will be looking at making a script just to get us started.

Enabling Properties and The Explorer

We will first go through on how you can enable your properties and explorer tab so we can. See where to locate things and mostly where to place your scripts.

Head over to your View Tab

Output

Now click on both Properties and The Explorer Icons to enable them.

Explorer

Inserting your first script.

If you have followed the steps above correctly you should now be able to see both your explorer and properties Windows open. Now head over to the category that says "ServerScriptService" and Insert your first script. Example can be seen below.
Inserting Script
Now that you have your first script inserted. You can run it by clicking the run Icon at the top of your home window nect to "Model"
Comments
In LuaU comments can be created by using "--" or "--[[]]". Comments are automatically ignored when running your scripts, and are used for leaving notes that you can refer to later on or even display diffrent sections of you code.

Writing inline comments

        
--print("Hello, world!")
        
    
The comment written above will be ignored when running your script.

Writing inline comments Example 2

        
--print("Hello, world!")
print("Luiboard is the best.")
        
    
On our second exmaple will only print out "Luiboard is the best." This is because our first print statement was commented and so ignored.

Writing Block comments

A block comment is a coment where you have two or more lines of code commented out, the syntax for the block comment is "--[[" this needs to also be closed by "]]". This means that anything inside the block comment will be ignored when running your script.
        
--[[
print("Hello, world!")
print("Luiboard is the best.")
]]
        
    
Variables
Variables are containers storing data values. In Lua and LuaU you do have both global and local variables. Local variables are variables that are defined with the "local" statement, while global variables do not need to be defined with the "local" statement.
        
person = "Lui" -- Global Variable
local Child = "trixx" -- Local Variable

You should also note that local variables can be global variables if they are places within the main code block,
explained below.
        
    

Local Variables

Local variables are only available inside the block they get defined in.
        
local Toys = 5 -- Local Variable

if(Toys > 3)then
    local Cars = 8 -- This variable will only be available on this if block statement.
    print(Toys,Cars) -- This will print the number of Toys and Cars
end
print(Toys,Cars) -- Since The Cars Variable is scoped to the if block statement above this will print out Toys and nil.
        
    

Global Variables

When dealing with Global variables, this can be accessed anywhere on the script.
        
Toys = 5 -- Local Variable
Cars = nil -- Setting the variable to nil by default

if(Toys > 3)then
    Cars = 8 -- This variable will only be available on this if block statement.
    print(Toys,Cars) -- This will print the number of Toys and Cars
end
print(Toys,Cars) -- Since The Cars Variable is now a global variable this can be accessed anywahere and this like right here will print out cars and Toys
        
    
Data Types
Variables hold datas of different types.

nil

"nil" is a data type that represents data that has no value.
        
print(Cars) -- This will print out nil the global variable car does not hold any value
print(type(Cars)) -- Prints out "nil"
        
    

boolean

"boolean" is a data type that has two possible values with are true and false. Booleans are mostly used for condition checking.
        
local Ready = true
if(Ready)then 
    print("Yes we are ready.") -- since ready is true this will print out "Yes we are ready."
    print(type(Ready)) -- Prints out "boolean"
end
        
    

number

"number" is a data type that represents a real double precision floating points.
        
print(type(5)) -- Prints out "number"
        
    

string

"string" is a data type that represents an array of characters.
        
print(type("Hello from Luiboard!")) -- Prints out "string"
        
    

function

"function" is a data type method that runs when it's called.
        
function Greetings()
    print("Hello from Luitrixx!")
end
print(type(Greetings)) -- Prints out "function"
        
    

userdata

"userdata" is a data type that represents an arbitrary data. This can be a set of predefined data types.
        
local Part = Instance.new("Part")
print(type(Part)) -- Prints out "userdata"
        
    

thread

"thread" is a data type that represents an new thread such as coroutines.
        
print("Hello from Luitrixx")
local MyNewThread = coroutine.create(function()
    while true do
        wait()
        print("Hello...")
    end
end)
print(type(MyNewThread))-- Prints out "thread"
        
    

table

"table" is a data type that represents arrays, sets,trees,dictionaries etc. Table data types can hold any value execpt "nil".
        
local NumbersTable =  {1,2,4,5}
print(type(NumbersTable))-- Prints out "table"
        
    
Operators
An operator is a symbol that tells the interpreter to perform specific mathematical or logical manipulations. This operations can be performed on both Variables and values

Arithmetic Operators

  • + [Addition]
  • - [Subtraction]
  • * [Multiplication]
  • / [Division]
  • % [Modulus]

Relational Operators

  • == [Equals to]
  • ~= [Not Equals to]
  • > [Greter Than]
  • < [Less Than]
  • >= [Greter Than or Equal to]
  • <= [Less Than or Equal to]

Equals Equals

        
local Cars = 5
local People = 5

if(Cars == People)then
    print("The number of cars is equal to the number of people.")
end
        
    

not Equals

        
local Cars = 5
local People = 6

if(Cars ~= People)then
    print("The number of cars is not equal to the number of people.")
end
        
    

Greater Than

        
local Cars = 10
local People = 6

if(Cars > People)then
    print("The number of cars is greater than the number of people.")
end
        
    

Less Than

        
local Cars = 2
local People = 6

if(Cars < People)then
    print("The number of cars is less than the number of people.")
end
        
    
Tables
Tables are used to store multiple data values. When creating tables you would start of with "{" and close it by "}". If you're adding multiple values to your table you separate them by a comma ",".
        
MyTable = {"Cake","Cinnamon Buns","Chocolate"}
        
    
Above we created our Table holding some string values of things I think of as sweet.

Accessing Items on a Table

Items in a table are indexed, so this means that you can access them by thier index.
        
MyTable = {"Cake","Cinnamon Buns","Chocolate"}
print (MyTable[1]) -- Prints out "Cake"
        
    

Adding Items to a Table

When adding items to a table we use "table.insert()". This methods takes is 3 arguments. The first argument is the table we are trying to add to. The second argument is Items we want to add and the third argument is the position on where we want the item to be placed on our table. By default the position is -1 which is at the end of the table.
        
MyTable = {"Cake","Cinnamon Buns","Chocolate"}
table.insert(MyTable,"Cookies")
print (MyTable[4])-- Since we added "Cookies" to our table it automatically takes the index of 4 thus far we can print it out.
        
    

Removing Items from a Table

When adding items to a table we use "table.remove()". This methods takes is 2 arguments. The first argument is the table we are trying to add to. The second argument is the position of which Item we want to remove.
        
MyTable = {"Cake","Cinnamon Buns","Chocolate"}
table.remove(MyTable,3)
print (MyTable[3])-- Since we removed the third Item on the table this will return nil because "Chocolate" does not exsist anymore  

    

Creating dictionaries in a table

When creating dictionaries, you can index your items with any data type except of nil, So this means instead of having the default numerical index you can have strings, booleans etc.
        
MyDict = {
    ["Users"] = {
        "Lui",
        "Kenny",
        "Awesomely"
    }
}
print(MyDict["Users"])--Since dictionaries can use any datatype as an index we can get our users like so.
--//Also as our users data is a table we can get the first indexed users just as we would in a normal table.
print(MyDict["Users"][1]) -- This will now print out "Lui"

    

Changing values in dictionaries

Changing values in dictionaries is a simple as changing a varible, As disctionaries can be indexed with other datatypes expect nil.
        
MyDict = {
    ["Users"] = {
        "Lui",
        "Kenny",
        "Awesomely"
    }
}
MyDict["Users"][1] = "Trixx"
print(MyDict["Users"][1]) -- This will now print out "Trixx" as we did change this value above.
--// We can also just go a head an change the whole users value to something else.
MyDict["Users"] = "Heyo" -- Now we have changed the value of Users to "Heyo".

    
If/Else Statements
In if statements we mostly use Realtion Operators, Which you can see more about them under. The Operators category. If you want to learn more about If statements please head over the Operators category

Else statements

Else statements are mostly used in cases where the first condition of a statement is not true.' Let's see an example below.
        
local Player1ID = 13712932
if(Player1ID == 476284020)then
    print("Player1 user's ID is 476284020")
else
    print("Player1 user's ID is not 476284020")
end
        
    
Since Player1:s user is is clearly not 476284020 that makes the first conditional statement to false. This will now lead us to our else statement where the output will print out "Player1 user's ID is not 476284020". Just note: Else statements are taken into account whenever the condition above is not true.

Elseif statements

Elseif statements are pretty much similar to if statements. This are statements that can be used as a seconddary statements.
        
local Player1ID = 13712932
if(PlayerID == 476284020)then
    print("This user's ID is 476284020")
end
if(Player1ID == 13712932)then
    print("This user's ID is 13712932")
end

--// Instead of making multiple if statements this can be chunked down with an Elseif statement

if(PlayerID == 476284020)then
    print("This user's ID is 476284020")
elseif(PlayerID == 13712932)then
    print("This user's ID is 13712932")
end
        
    
While Loops
While Loops are used to run a specific code as long as the statement given is true. Note: When running while loops you need to include a "wait()" as this may cause a timeout error.

While Loops

        
local Coins = 1
while (Coins < 10) do
    wait() -- Always include a wait function when running while loops,to prevent a timeout exhaustion error.
    print(Coins)
    Coins+=1 --Incrementing the coins amount by one
end
        
    
For Loops
For Loops are used to iterate over tables, dictionaries and lists. When using for loops you have two different arguments you need to pass in, One being the Index(Position of the Item in the Table array) and the other Option being the actual Value In most case instead of always typing out Index,Value you can use (i,v). Note this arguments are like variables and can be named anything I'll show you an example below with the table we created under the tables Category.

For Loops

When using For Loops you may have to use either "pairs()"-- This is used mostly in disctionaries but still works with numeric tables or you could use "ipairs()"-- This is used mostly in numerical tables
        
MyTable = {"Cake","Cinnamon Buns","Chocolate"}
for i,v in pairs(MyTable) do -- i(Being the index) and v(Being the value)
    print(i,v) -- This will print out the position and the value of Items in the table.
end
        
        
--//Output:
--[[
  1 Cake
  2 Cinnamon Buns
  3 Chocolate
]]
        
    

For Loops pairs()

        
local MyTable = {
    2,3,4,5,6
}
local MyDict = {
    [1] = "a",
    [2] = "b",
    ["Name"] = "Luiboard"
}
--// Since "MyTable" above is a numerical table when we loop through it using pairs() we will get bot the index and value as thier index is a number
for index,value in pairs(MyTable) do -- i(Being the index) and v(Being the value)
    print(index,value) -- This will print out the position and the value of Items in the table.
end

--// Though "MyDict" table above is not a numerical table when we loop through it using pairs() we will get bot the index and value.
for index,value in pairs(MyDict) do -- i(Being the index) and v(Being the value)
    print(index,value) -- This will print out the position and the value of Items in the table.
end
        
        
--//Output:
--[[
MyTable Output:
   1 2
   2 3
   3 4
   4 5
   5 6

MyDict Output:

  2 b
  Name Luiboard
  1 a

As you can see we both got the Index and the value printed out.
]]
        
    

For Loops ipairs()

We are going to use the tables we constructed above but instead of pairs we are going to use "ipairs()"
            
    
for index,value in ipairs(MyTable) do -- i(Being the index) and v(Being the value)
    print(index,value) -- This will print out the position and the value of Items in the table.
end

for index,value in ipairs(MyDict) do -- i(Being the index) and v(Being the value)
    print(index,value) -- This will print out the position and the value of Items in the table.
end
--//Output:
--[[
MyTable Output:
   1 2
   2 3
   3 4
   4 5
   5 6

MyDict Output:

  2 b
  1 a

When you look at MyDict output you can see we do not have "Name Luiboard" printed out. This is because ipairs is only used for tables with numerical indexes.
]]
        
    
Functions
Functions are simply block of codes that only run when called. When using functions you can pass though arguments also known as parameters. Functions not only run a block of code, but can also return back results. Just as variables functions can be created both globally and locally.

Creating Functions

        
function Greetings()
    print("Hello there!")
end
        
    

Calling Functions

        
function Greetings()
    print("Hello there!")
end
Greetings() -- This will call out function above resulting into a print of "Hello there!"
        
    

Passing Arguments

        
function Greetings(User)
    print("Hello there! "..User) -- ("..") are used as concatination operators to concatenate two or more strings together!
end
Greetings("Luitrixx") -- This will call out function above resulting into a print of "Hello there! Luitrixx"
--// Bonus
Greetings("Tom")
Greetings("Builderman")