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.
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.
In this section you will learn the Basics needed to create a sucessful script.
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.
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 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 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.
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.
There are 3 types of variables in Luau.
Example:
local x = 1
y = 1
_G.z = 1
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
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 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.
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!
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!
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 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 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 |
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
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
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!
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).