# Intro to Luau: A Beginner's Guide ###### Tags: Roblox, Luau, Roblox Scripting, Scripting, Programming, Roblox Code - Table of contents: [ToC] In this HackMD, you will be learning what Luau is, the basics of Luau (Roblox-Lua, RBX.lua), and some reliable sources on learning Luau. ## What is Luau? Luau (pronounced Loo-uh U) is a fast, small, safe, gradually typed embeddable scripting language derived from Lua. It is used by Roblox game developers to write code, as well as by the Roblox Engineers to write core mechanics for the editor (Roblox Studio). Luau uses Lua 5.1. To check which version of Lua Luau is using, you could run: ```lua= print(_VERSION) --> Output: Lua 5.1 ``` in the command bar, or just go to https://luau-lang.org/ (The official Luau website) for more information. ## Basics of Luau In this section you will learn the Basics needed to create a sucessful script. ### Comments Luau code will ignore certain lines if prefixed with certain characters. In comments, you can write anything and it doesn't need to follow the code grammar / rules. Single line comments are prefixed with ``--`` while Multiline comments are prefixed with ``--[[`` and end with ``]]``. Example: ```lua= -- This is a comment! print('Hi!') -- Comments are ignored while code still works. --[[ This is a Multiline Comment! ]] ``` You can also use multiline comments for a single line. This is useful for having the code ignore certain parts of the line without ignoring the whole line. Example: ```lua= print(1 + 1 --[[* 2]]) -- Output: 2 ``` In the code above, the output is 2. Why? Well because the '* 2' portion is ignored, so it wouldn't multiply by 2 and only add 1 by 1. ### Services A service, in scripting, is an object that contains built-in properties, functions, events, and callbacks. These services are used to reach a goal. For example, MarketplaceService is used to handle in-game purchases, while HttpService is used for getting information from external websites. A list of Roblox Services can be found [here](https://roblox.fandom.com/wiki/Category:Services). To get a service with a script on Roblox, you would need to use: ``ServiceProvider:GetService()`` Example: ```lua= local Marketplace = game:GetService('MarketplaceService') ``` Using that code as an example, you could then use the functions, events, properties, etc, of the service. For more information on services and their uses, go to the [Roblox API](https://developer.roblox.com/en-us/api-reference). ### Scripts Scripts are containers for Luau code. The default content for scripts when you create a new Local/Server Script is: ```lua= print('Hello, world!') ``` The default content for a Module Script is: ```lua= local module = {} return module ``` There are 3 types of scripts: - Local Scripts (LocalScript) - Server Scripts (Script) - Module Scripts (ModuleScript) Local Scripts are used for executing Luau code on the client. Server Scripts are used for executing code on the server. Modules Scripts are a bit different, though. Module Scripts **do not** execute by themselves when the game has started running. Module Scripts need to be required by either a Server Script or a Local Script in order to be run. Local Scripts **do not** run when parented by a service owned by the server. (ServerScriptService, ServerStorage, Workspace, etc) Server Scripts **do not** run when parented by a service owned by the client. (ReplicatedFirst, StarterPack, StarterGui, etc) ReplicatedStorage is a neutral service, allowing both Server and Client to run. ServerScriptService and ServerStorage **is** invisible to the client, and ReplicatedFirst **is** invisible to the server. - More information on Local Scripts can be found [here](https://developer.roblox.com/en-us/api-reference/class/LocalScript). - More information on Server Scripts can be found [here](https://developer.roblox.com/en-us/api-reference/class/Script). - More information on Module Scripts can be found [here](https://developer.roblox.com/en-us/api-reference/class/ModuleScript). ### Variables A variable is a name that holds a value. Variables can be either be a [Number](https://developer.roblox.com/en-us/articles/Numbers), [String](https://developer.roblox.com/en-us/articles/String), [Boolean](https://developer.roblox.com/en-us/articles/Boolean), [Data Types](https://developer.roblox.com/en-us/api-reference/data-types), etc. There are two scopes for variables: Local, and Global. #### Defining Variables There are 3 types of variables in Luau. Example: ```lua= local x = 1 y = 1 _G.z = 1 ``` - _G. Variables **do not** follow Scopes and can be used across scripts. - Global Variables **do not** follow Scopes and can only be used in the script it is defined in. - Local Variables **do** follow scopes and can only be used in the script it is defined in. You can also define multiple variables in a single line. Example: ```lua= local x, y, z = 1, 2, 3 ``` OR ```lua= a, b, c = 1, 2, 3 ``` ### Scopes ###### Figure 1 (Credits: Roblox Developer API) ![](https://developer.roblox.com/assets/bltd46ee264544ef2f3/Scope-Diagram.png) - Block B **can** access the local variable in block A. - Block C **can** access the local functions/variables in blocks A and B. - Block A **can not** access the local functions/variables in blocks B and C. - Block B **can not** access the local functions/variables in block C. Example: ```lua= local function foo() local name = 'Bob' print(name) --> Output: Bob end print(name) --> Output: nil ``` More information on Scopes can be found [here](https://developer.roblox.com/en-us/articles/Scope). ### Functions Functions are sets of instructions that can be used multiple times in a script. A function can be executed through a command or triggered through an event. #### Defining Functions A basic function declaration uses the ``function`` keyword, following with the name of the function and ending with two parenthesis ``()``. Example: ```lua= local function foo() --> function name: 'foo' -- Function body end ``` When called, functions will execute the code in the function's body. To call a function, first make sure your function is in the scope of your script, then type the function's name and end with two parenthesis: ``foo()`` Example: ```lua= local function foo() print('This function has been called!') end foo() --> Output: This function has been called! ``` #### Function Arguments & Parameters A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that is being sent to the function whenever the function is called and executed. Example: ```lua= local function foo(text) print(text) end foo('Hi, my name is Bob!') --> Output: Hi, my name is Bob! ``` #### Returning Values Return is widely used for returning a value. Example: ```lua= local function add(number1, number2) return number1 + number2 end local newNumber = add(1, 3) print(newNumber) --> Output: 4 ``` ### Events Events send out signals when specific things happen in a game. For example, if a player touches a part, it would fire the ``Part.Touched`` event. Example: ```lua= local Part = game.Workspace.Part Part.Touched:Connect(function(hitPart) Part.Color3 = Color3.new(255, 0, 0) end) ``` In the example above, the ``.Touched`` function will only fire whenever another Part touches the part. More information on Events can be found [here](https://developer.roblox.com/en-us/articles/events). ### Conditional Structures Conditional structures allow scripts to perform actions when specific conditions are true. Conditions can be checked with the **relational operators** list summarized here: | Operator | Description | Example | | -------- | ----------- | --------------- | | == | Equals to | 1 == 2 🠒 False | | ~= | Not Equals to | 1 ~= 2 🠒 True | | > | Greater Than | 1 > 2 🠒 False | | < | Less Than | 1 < 2 🠒 True | | >= | Greater Than or Equals to | 1 >= 2 🠒 False | | <= | Less Than or Equals to | 1 <= 2 🠒 True | #### If Statement Booleans, Strings, Data Types, and Numbers can all use the **relational operators**. Booleans and Strings can only use the ``Equals To`` and ``Not Equals To`` operators. Example: ```lua= local Number = 10 if Number == 10 then print('The number is ten!') --> Output: The number is 10! end ``` You can also do the same for Booleans. Example: ```lua= local Boolean = true if Boolean == true then print('True!') --> Output: True! end ``` You can do the same with Strings. Example: ```lua= local String = 'Hi!' if String == 'Hi!' then print('Hi!') --> Output: Hi! end ``` ...And finally, you can do the same with Functions. Example: ```lua= local function foo() return true end if foo() == true then print('True!') --> Output: True! end ``` #### Else Statement There are also ``else`` statements in Luau. ``else`` is only executed when the conditions are not met. You **cannot** start a conditional structure with else. Example: ```lua= local Boolean = false if Boolean == true then print('True!') else print('False!') end --> Output: False! ``` In the example above, it will print 'False!' because the Boolean variable is false and the if statement will only run if the Boolean variable is true. #### Elseif Statements ``elseif`` is basically another ``if`` statement. You **cannot** start a conditional structure with ``elseif``. Example: ```lua= local Number = 10 if Number == 1 then print('One!') elseif Number == 10 then print('Ten!') else print('It is not ten nor one. :(') end --> Output: Ten! ``` ## End Thank you for reading ``Intro to Luau: A Beginner's Guide``! I hope this HackMD has helped you, and I wish you good luck to your scripting career! :D Thanks, Luke (DemolishSanity). ## Sources / Reliable Websites - [Roblox API](https://developer.roblox.com/en-us/api-reference) - [Stack Overflow](https://stackoverflow.com/) - [Roblox Fandom](https://roblox.fandom.com/wiki/) - [Official Luau Website](https://luau-lang.org/)