Hey Developers,
This is a beginner tutorial to get started with Lua.
Table of contents:
Introduction
So this is the start of our Tutorial. So get prepared because this will be long.
If you want to get started with programming, you will need to practice loads, and I mean LOADS.
Getting Started
Alright, so first thing to do is open Roblox Studio, which we all should know how to do.
Now we need to create a Baseplate:
I recommend using either Classic or Non-Classic Baseplate.
Just so you know, when setting a value for a variable or property, we use:
String: "Hello World!"
Int or Number: 0 or 0.1
Bool: True or False
Now that you have opened a fresh Baseplate, we need to open up the essential stuff for programming.
So open these tabs up:
Now that you have opened these tabs, next to ServerScriptService, press the + button. This will take you to the Add Object menu. Now click the Script button. This will create a new script.
Printing
Now that you have opened a new script, you can see writing in it. This is the starter code when you create scripts:
print("Hello World!")
This code prints it (not literally) to the Output:
There is also other ways of printing:
- Variables (I will show in the next topic)
- Operations
- Strings
Operations:
+ = Add
- = Subtract
X or * = Multiply
/ = Divide
print(1+1) -- Prints 2
print(1-1) -- Prints 0
print(1*1) -- Prints 1
print(1/1) -- Prints 1
Strings:
You can add 2 strings or more together and then printing it out to the Output.
You can do so with the operator +, as it means to add stuff together:
print("Hello " + "to all people seeing this!")
You could also do this with numbers as well:
print("1" + "1") -- Prints 11
If you ever want to connect a string and number together, you would need to write the string in “” and the number on its own. Here is an example:
print("You have " + 5 + " spoons!")
Output:
You have 5 spoons!
Test Time!
Say you want to print a score count that says "You have been awarded " and the number or points you have. For this one, you will have 7 points. Try doing this by yourself. If you have Roblox Studio, great! You will need this to write down the code. Create a script in ServerScriptService. If you don’t have Roblox Studio, you might want a pen and paper for this to write down the code:
If you are stuck, you can use a hint.
Hint
Use your knowledge of connecting a string and number together. Use “” and spaces when needed. You can always go back and refresh your mind.
Answer
print("You have been awarded " + 7 " points!")
Output:
You have been awarded 7 points!
Congrats! You have now learned how to use the print command.
Variables
Now that you have learned to use the print command, we will be moving onto Variables.
Variables are like boxes, and those boxes store the value. We can access the value by declaring the variable by its given name. We can create variables by writing:
local myVariable = 20 <-- Value as Int/Num. myVariable can be changed to anything.
There are 2 types of variables. Those are Local and Global. Local is when the variable is visible for only what its inside:
-- Don't worry about this code, this is just an example.
local function Print(Text)
local statement = Text -- Inside the scope
end
This example shows that the variable can only be accessed in the function, nowhere else.
Global means that it can be accessed anywhere, basically the opposite of local:
local statement = "Hello World!" -- Outside the scope
local function print(Text)
print(statement) <-- Variable from outside of scope
end
You can also print variables:
local word = "Hello World"
print(word)
Output:
Hello World
You can also do sums with variables:
local num1 = 1
local num2 = 2
print(num1 + num2)
Output:
3
Also, you can add words or phrases together using the plus sign:
local word1 = "Hi"
local word2 = "There!"
print(word1 + word2)
Output:
Hi There!
You can also you 2 dots to add them together:
print("Hi " .. "There!")
Output:
Hi There!
Test Time!
Say you want to print out a statement saying you have 5 dogs and 3 cats. How would you write your code?
Hint
Write down 2 variables with the names Dogs and Cats. State the value of the variable. Then print it out with strings saying "You have " + number of dogs + " and " + number of cats. This may look like a lot but it will be very simple when you get used to it.
Answer
local numofdogs = 5
local numofcats = 3
print("You have " + numofdogs + " dogs and " + numofcats + " cats."
Output:
You have 5 dogs and 3 cats.
Congrats! You have now learned how to use variables.
Loops
Now that you have learned about variables, we will be moving on to Loops.
Loops are what we can use to repeat something for a certain amount of times.
There are 3 ways to write loops, those are:
- For Loops
- While Loops
- Repeat Loops
Say we want to loops through an amount of numbers, then print them out to the output. That’s when the For Loop gets in:
for number = 1, 10, 1 do -- 1: Starting number 10: Ending number 1: How much we add to each number.
print(number)
end
Output
1
2
3
4
5
6
7
8
9
So, now we will be getting all children from a folder and printing it out to the output. In the folder, there are 3 parts, Part1, Part2 and Part3:
local folder = game.Workspace.Folder
local Numbers = folder:GetChildren()
for i = 1, #Numbers, 1 do
print(i, Numbers[i].Name)
end
Output:
1 Part1
2 Part2
3 Part3
How the for loop works:
- The i variable is where we start from. So we start from 1.
- The #Numbers part is where we will end. So we end at 3.
- The 1 at the end is optional. It it the increment (How much we go up by).
Test Time!
Say I change the amount of parts so we have 10 parts and I replace the increment so it goes up by 2. What will happen in the output now?
Hint
You can copy the code from the loop above, changing the #Numbers to 10 and the increment number to 2. Then you can print out i
Answer
local folder = game.Workspace.Folder
local Numbers = folder:GetChildren()
for i = 1, #Numbers, 2 do -- Table = 10
print(i, Numbers[i].Name)
end
Output:
1 Part1
3 Part3
5 Part5
7 Part7
9 Part9
Congrats! You have now learnt how to use 3 types of loops.
Functions
Now that you have learned about loops, we will be moving on to Functions.
Functions are sets of instructions the tell something what to do.
To run a function, we must declare it underneath it:
local function
An example for washing hands would be:
Rinse hands
Apply soap
Rub thoroughly
Rinse hands again
Dry hands
Now if you wanted to write that out in code by using prints, you would do:
local function WashHands() -- Name of function
print("Rinse hands")
wait(1) -- Wait number of seconds
print("Apply soap")
wait(1)
print("Rub thoroughly")
wait(1)
print("Rinse hands again")
wait(1)
print("Dry hands")
end
WashHands() -- Call the function
Output:
Rinse hands
-- Wait 1 second
Apply soap
-- Wait 1 second
Rub thoroughly
-- Wait 1 second
Rinse hands again
-- Wait 1 second
Dry hands
Just so you know, wait is when the code reads line-by-line and gets to that line, it will wait however many seconds you give it.
Now we can move on to Parameters
Parameters are special types of variables that pass information through functions.
When it goes through, we can call that an argument.
If you wanted to make a function to solve a math addition problem, we could do:
local function SolveProblem(num1, num2)
print(num1 + num2)
end
SolveProblem(1, 19) <-- 20
Output:
20
Test Time!
What if you were to create your own print function. How would you create it as your own function while using parameters.
Hint
You can create a function called Print with a parameter called Text. Then in the function, you could print out Text (the parameter)
Answer
local function Print(Text)
print(Text)
end
Print(Hello World!)
Output:
Hello World!
Built-In Functions
Built-In Functions is what Roblox put in to scripting to make it simple for developers to use. The Built-In Functions are:
- WaitForChild()
- GetChildren()
- FindFirstChild()
- GetService()
- Clone()
- Destroy()
If you want to learn more built-in functions, DM me on the devforum and I’ll be sure to add it to this tutorial.
WaitForChild:
WaitForChild is the function we use to wait for a certain object in your workspace to load into the script so we can access it.
So, say we are going to make a function (Go back to functions if you don’t understand) which is going to print out the name of the object:
local part = game.Workspace:WaitForChild("Part") -- We would write in the brackets, the name of the object, for this, we are saying Part.
local function PrintName()
print(part.Name)
end
PrintName()
Output:
Part
GetChildren:
GetChildren is a function we use to essentially get all children from an object. So, now we will be getting all children from a folder and printing it out to the output. In the folder, there are 3 parts, Part1, Part2 and Part3:
local folder = game.Workspace.Folder
local Numbers = folder:GetChildren() -- Gets all objects in the folder
for i = 1, #Numbers, 1 do
print(i, Numbers[i].Name)
end
Output:
1 Part1
2 Part2
3 Part3
FindFirstChild:
FindFirstChild is the function we use to find an object in a specific place. For example if I want to find the humanoid in a player, we would do:
local humanoid = game.Players.LocalPlayer:FindFirstChild("Humanoid")
FindFirstChild is used to find certain objects. For example, if there are multiple parts in a folder, you can call this function to, like I said before, find a specific object in this folder:
local folder = game.Workspace.Folder
local event1 = folder:FindFirstChild("Event1") -- Direct connection to the object so you can use this variable somewhere else in the script.
GetService:
GetService is the function we use to get services like ReplicatedStorage, Players. We use this function because some Services like Players, TweeningService and much more aren’t in the Explorer Tab so we can’t reference them without stating them in a variable. Here is an example of using GetService() in a script:
local UIS = game.UserInputService -- You can't do this as there is no UserInputService in the Explorer Tab
local UIS = game:GetService("UserInputService") -- You can do this as it is efficient and won't cause any errors unless there is a typo
Clone:
Clone is the function we use to duplicate objects. We can use it in scripts that deal with parts and objects like values etc. For example if I want to clone an object, I would do:
local part = game.Workspace.Part
part:Clone()
There is not much to say about this function as it is very simple.
Destroy:
Destroy is the function we use to DESTROY things. MWAH- Oops, nearly let my evil side get past. This function can be used to deal with parts and other objects such as values. For example if I want to DESTROY an object, I would do:
local dumbpart = game.Workspace.Luxurious_Scripter -- Um... Let's just continue
dumbpart:Destroy() -- Just realized I am destroying myself...
Congratulations! You have now fully learnt about Built-In Function. You may feel like you know everything off by heart but you need to practice it A LOT. Anyways, now we will be moving on to the next topic.
Table
Now that we have learnt about functions, let’s move onto Tables.
Tables can store multiple types of values such as numbers, booleans, strings and functions. You can use Tables in 2 different ways:
- Array
- Dictionary
About Arrays:
An array is an ordered list with values within it. These values can vary between any type of value from the list that I mentioned above.
To create an array, we would create it as a variable and then add curly brackets surrounding each data value:
local myTable = {"Data1", 21.5, game.Workspace.part}