Try โ€‚โ€‰HackMD

Intro to Luau: A Beginner's Guide

Tags: Roblox, Luau, Roblox Scripting, Scripting, Programming, Roblox Code

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:

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:

-- 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:

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.

To get a service with a script on Roblox, you would need to use:
ServiceProvider:GetService()

Example:

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.

Scripts

Scripts are containers for Luau code. The default content for scripts when you create a new Local/Server Script is:

print('Hello, world!')

The default content for a Module Script is:

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.
  • More information on Server Scripts can be found here.
  • More information on Module Scripts can be found here.

Variables

A variable is a name that holds a value. Variables can be either be a Number, String, Boolean, Data Types, etc.

There are two scopes for variables: Local, and Global.

Defining Variables

There are 3 types of variables in Luau.

Example:

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:

local x, y, z = 1, 2, 3

OR

a, b, c = 1, 2, 3

Scopes

Figure 1 (Credits: Roblox Developer API)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More โ†’

  • 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:

local function foo() local name = 'Bob' print(name) --> Output: Bob end print(name) --> Output: nil

More information on Scopes can be found here.

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:

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:

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:

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:

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:

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.

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:

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:

local Boolean = true if Boolean == true then print('True!') --> Output: True! end

You can do the same with Strings.

Example:

local String = 'Hi!' if String == 'Hi!' then print('Hi!') --> Output: Hi! end

โ€ฆAnd finally, you can do the same with Functions.

Example:

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:

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:

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