devforum-roblox-com-t-lua-scripting-starter-guide-394618
devforum-roblox-com-t-lua-scripting-starter-guide-394618
Author’s Note
► Click to open the letter
P.S. I’ve tried to make this tutorial as kid-friendly as possible, if it is still difficult to read or 20 / 23
understand, please DM me on any platform and I’ll try my best fix it. Specific examples are Nov 2021
appreciated.
Oct 2022
The Basics of Roblox Lua
Introduction
Chances are, if you’ve ever played a game, you’ve wondered how it was made. How does a sword go
from unequipped to swinging in just a matter of seconds? Why? How is this app able to stream the
movies I select? How is the website we’re currently on even able to operate?
Computer programming is an essential part of modern life, as it determines and allows for many
technologies we use on a daily basis to operate. However, all code is not written in the same way,
rather, there are distinct languages, or ways of writing code. In fact, you might already be familiar
with the names of various programming languages, including but not limited to JavaScript or
Python.
In this tutorial, we’ll be covering Lua, the official language of Roblox which can be used to create a
variety of different games for users to play.
Format
In this tutorial, we’ll be using a specific format to write, input, and receive the result of our script. To
use a format identical to mine, click on View in the upper left side of your screen, then make sure
you have selected and have the following settings:
Creating a Script
Scripts can be inserted almost everywhere, for the sake of keeping this tutorial short, head over to
Model on the top panel and browse to the far left and click on Script, which will generate a script
that we can use for our tutorial.
Strings
Type out print in all lowercase letters. If print is typed incorrectly or typed with any capitals, the
program will not recognize the command and will not run it. Add a pair of parenthesis after, studio
should automatically add the second one. Make sure there is no space in between print and the
parenthesis otherwise Lua will not be able to read it.
print()
Next, we need to add a string in-between the parenthesis so that we don’t just print empty space. A
string is any character or words inside of a pair of quotes, such as “Hello World!” For our tutorial,
we’ll be adding the string “YellowNoobs!” for the program to print.
print("YellowNoobs!")
You may replace "YellowNoobs" with any other text you wish as long as it is in-between the
parenthesis (""), the end result should look something like this,
If you want to include quotation marks in your quote, you can also use a pair of single quotes (’’)
and put the double parenthesis, or vice-versa for single quotes to be printed.
Print also works for numbers and equations, which it’ll automatically simplify in the output. But
before we write out our problem, we need to find the symbols needed to get the right answer.
Arithmetic operators are basically all the tools we use in programming to indicate a relationship
between numbers. This sounds complicated, but it’s just what we call the symbols we use to add,
subtract, multiply, and divide numbers.
Addition: +
Skip to main content Subtraction: -
Multiplication: *
Division: /
Exponents: ^
Modulus (Remainder After Dividing a Number): %
Now, we’ll write our equation in-between the parenthesis, and paste it into the command bar once
we’re ready to get the output. Feel free to mess around with the operators to get the hang of it.
If we put our code into the command bar we’ll get 81 instead. This is the answer to the equation, and
Lua does this because it’s a simplified version, or answer to the problem we originally printed. An
important thing to keep in mind is that Lua completes the math using the Order of Operations,
meaning it starts with exponents, then division and multiplication, and finally addition and
subtraction.
print(1 + 5 * 2)
While you may be tempted to say the answer is 12, since you add 1 + 5, getting 6 and multiplying it
by 2, this is wrong. Instead, you multiply 5 and 2 first, then add 1, getting 11. This is a rule of both
math and programming, so practice the concept until you can avoid printing the wrong number.
Concatenation
In order to combine two or more separate values together, you can use concatenation in the print
function. We’ll still use the same format, but will add two periods or dots after each string. Do not
place this inside of the string, otherwise Lua will interpret it as part of the string and produce an
error.
If we paste this into the command bar, the output will show us a full statement as if it was a single
string, meaning the concatenation was successful.
Variables
Now that you’ve learned how to make a script, you’ll learn what variables are and how to make your
own. Variables are considered vital because they save a lot of time and save you time and make
your scripts simpler to review. They can be booleans, numbers, strings, or practically anything you
want to assign a name to.
If you’re struggling to understand the concept of a variable, you can compare it to a nickname you
might give someone who’s name may be too tedious to pronounce. Although it is not their
birthname, calling them by their nickname will still get their attention as they’ve been given and
called by it before.
Below, I’ve given an example of a variable. The term “variable” will be used interchangeably with the
string “YellowNoobs!” whenever we use it in the script. If we put the variable inside of the print
command, it will print the text assigned to it. Since the variable is identical to the string we assigned
it, we do not need any quotes around the variable name. If you put quotes around a variable name,
Lua will interpret it as a string, and print the actual name rather than the value.
variable = "YellowNoobs!"
print(variable)
If we copy and paste the code into the input, the output will print our statement.
It is necessary that you define your variables before using them in your code. Since Lua reads code
from top to bottom, it will not know what the variable stands for when it executes the print function.
If we paste the code into our command bar, we’ll get the output nil, which means nothing or no data.
Whenever we define the variable, its scope covers all of the code below in the script. Variable scope
is just another word for when the variable can be accessed in your script.
There are two types of variables which you’ll hear about, local and global. The variables we’ve made
in this tutorial so far have been global variables, which means they can be accessed in all code
below inside the script. Local variables will be covered later in this tutorial, but in simplest terms,
they have a much more limited scope.
While you can almost freely name your variables, there are a few restrictions that exist inside Lua.
Alphabet Letters
Numbers
Underscores
Capital letters as the first letter of the variable (Not required, but common practice)
Numbers as the first letter of the variable
Spaces
Lua Reserved Keywords
► Keyword List
Comments
Comments are a simple green text that organize and give context to your lines of code, which can
be a useful tool for yourself or others if you plan on sharing it.
Creating comments is extremely easy, add - - and then your text after you are finished. It should
appear something like this.
If you want your comment to cover more than one line, insert brackets, with your text inside.
Any text attached to the comment will not be executed by the program, and will only be seen by
the reader. This green text is helpful in case you want to find a specific piece of code in your script.
Comments are also crucial if you plan on sharing your scripts and want the reader to understand
what certain commands do. Besides context, you can use comments to credit yourself for your
script, or write out directions that the user who uses your script might want to see.
Properties
If you want to change the settings (such as the transparency, or invisibility) of an object in the
workspace, you have to write down the specifics before Lua knows what you want it to do.
So, if we want to change the transparency of the baseplate, we first need to define that the
baseplate is inside game.Workspace, with game being the game we’re working on and workspace
being the place where baseplate and the rest of our parts are stored. Then, we write the object we’ll
be changing (baseplate) and the setting that we want to change (its transparency), and then assign
a value to it. If we want it to be fully invisible, we’ll set the value to one, lowering this amount will
make it so that it’s semi-transparent.
game.WorkSpace.Baseplate.Transparency = 1
In the end, this should make the baseplate completely invisible if we run the code or play the game.
Keep in mind that we can change the properties of object in Studio as long as you define it
correctly in the code.
Functions
When you’re writing a sequence of code that needs to be repeated, it may be a little tedious and
also messy to write it out several times. Functions are a tool in Lua that we can use to put lines of
different code in, which can be repeated and manipulated a lot easier by us. The functions in this
segment are not to be confused with the built-in functions of Lua, such as print.
Creating Functions
To define a function, we need to write function and then assign it a name. Then, add a pair of
parenthesis after your function name without any spacing in-between, and press enter. I’ll be
naming my function printStuff, but you can name yours differently.
function printStuff()
end
Now, we need to write our code for our function to actually execute it. You’ll notice that when you
press enter after writing your function, you’ll automatically get end. This statement is here so that
you can end, or close off your function, otherwise Lua would get an error.
Skip to main content
I want to print several strings, so I’ll insert the several sequences of code inside my function.
function printStuff()
print("Hello World!")
print("It's a beautiful, sunny day outside.")
print("I like my coffee black, no sugar or cream.")
end
We now have our function, however, nothing will be happen since we haven’t written the command
to execute it. We need to type the function name on the next line, and Lua will print our strings.
function printStuff()
print("Hello World!")
print("It's a beautiful, sunny day outside.")
print("I like my coffee black, no sugar or cream.")
end
printStuff()
Now, Lua will execute our code. If we paste all of the content into the command bar, we’ll see that
our function is successful. It’s important to note that just like variables, functions need to defined
before we actually execute them in our code, otherwise we’ll just get nil.
When we covered local variables previously in our tutorial, I mentioned that the scope of it was
much more limited. Whereas global variables can be accessed anywhere in our script after being
defined, local variables are limited to a block.
So what is a code block? An example is basically the indented sequence of code that we see in our
function. I’ll attach comments to the parts that are a part of the block of code for a visual
demonstration.
function printStuff()
local myVariable = "Hello World!"--This is part of the code block.
print(myVariable)--This is also part of the code block.
end
print(myVariable)
You can also tell if something is a block by the small arrow that appears to the left. If you look
closely, the arrow facing down next to function is indicating that the block is being shown, and
when it is facing up, it means it is closed.
If we try to print the local variable outside the block, we’ll get nil since the variable can’t be
accessed.
A handy feature of functions is the ability to manipulate the variables with ease. You might’ve
already noticed the pair of parenthesis when we define the function, and the two other parenthesis
when we execute it. These are the spaces where we put our arguments and parameters.
Every time we execute a function, we might want to change certain parts of the code each time.
Parameters are used to tell Lua which variables (and thus any information) inside the function you
want to change. They are established when we first define our function, and are placed in-between
the first pair of parenthesis on the same line. Make sure to not define the variable you’ll be
changing inside of your function, otherwise the parameter will not work.
function printStuff(myString)
print(myString)
end
Now that we’ve defined the parameter, we need to write an argument for the function to be
properly executed. An argument is the value we assign the code when executing it. It comes inside
our next pair of parenthesis, and can be changed every time we execute the function separately.
We’ll print a string and a number, and the program should treat the argument as the variable in the
function.
function printStuff(myString)
print(myString)
end
printStuff("Hello World!")
printStuff(27)
Now if we put it in the output, the two values should be successfully printed.
Now if we paste it into our command bar, the output should print both statements in the order we
gave them. It is important that you put the arguments and parameters in the same order or you will
assign the wrong value to the variable. Also, you always have to define your parameters when you
execute your function, otherwise Lua will not know what to assign to the variables.
Local Scripts
You might’ve already noticed that below the option to create a script, there is something called a
local script. This tutorial won’t go too in-depth about them, but offer a broad summary about its
purpose.
Local scripts are scripts that only affect the user’s client rather than the server. A user’s client is
basically what they see, rather than the whole server. So whereas you might be seeing something,
your friend playing the same game as you might see something completely different. In
comparison, scripts that affect the server are seen by everyone, and not just the individual’s client.
The concept is a bit difficult to describe by text, and I still can’t do it justice by talking about it in
depth, so I urge you to look at this video by TheDevKing to get a better idea of what local scripts are
capable of.
Booleans
You might’ve heard this word in math class and it is related. Booleans in scripting are the conditions
true or false.
When comparing values you’ll get a Boolean as well, although you can simply write true or false too.
Relational Operators
Relational operators are the operators used to compare values with one another.
They include:
Equal to, not to be confused with equal which simply assigns something rather than
comparing two values: ==
Not equal to, which is true if the values are not equal: ~=
Greater than: >
Less than: <
Greater than or equal to: >=
Less than or equal to: <=
Now, let’s take a look at some numbers and see if they represent a true or false boolean
4 > 4
2 == 2
8 < 2
The first one is false, since the value can only be greater than, and not equal to the other. The next
one is true as the values are equal to one another. The last is clearly false, since 8 is not less than
the number 2.
Skip to main content
If Statements
If statements make it so that specific commands only happen if the condition is met, it will execute
the code.
For example, if 5 > 1 the Output will print “Congratulations” since 5 > 1 is true. If the script said 5 < 1
then the script will not print “Congratulations” in Output.
if 5 > 1 then
print("Congratulations")
end
This is equivalent to saying true instead and will still print our string.
if true then
print("Congratulations")
end
Loops
Sometimes, our code needs to be repeated in order to produce an outcome we’re looking for. Loops
are statements that allow us to repeat code multiple times. They come in several different forms,
which we’ll cover in this section of the tutorial.
While Loop
While loops repeat sequences of code while the statement is true. However, when the condition is
false, it will not execute the code block. For example, while a variable is less than a certain value, it
will continue to print out a string we decide to give it. This is just one of countless things you can do
with a while loop, so feel free to get creative if you’re feeling ambitious.
For our while loop, we’ll assign a number to our variable, x. Afterwards, we go on the next line and
write while. This helps Lua understand that it is a while loop, so that when the condition is true, it’ll
execute the code block. The do you see after our boolean is simply telling Lua to execute the block
of code that is part of the loop when the condition is true.
x = 10
In this code, I defined my variable as 10 and made it so that the while loop would continue to run
until it was no longer less than a 100. Each time it runs the code, I made it so that it would add 10 to
the variable, meaning that once it ran several times, it would no longer execute the code since the
condition is false.
Notice the string being printed after the loop. This is not executed until the loop is finished, so not
attaching it to the loop can be used to tell us when the loop is finished.
For Loop
For loops are able to loop as many times as the user wants. First, we define a variable for the loop,
and assign it to a starting and ending value. The loop will continue to repeat until the starting value
reaches the end value. While the amount the starting value increases is by 1, you can change it by
adding a comma after the end value. Below, is an example of a for loop.
for x = 1,25,5 do
print(x)
end
Our loop will now print 1, 6, 11, 16 all the way until 21 where it will stop since it can not go past 25.
for (VariableName) = (Start Value), (End Value), (Number value increases by)
(Statements)
end
Repeat/Until Loop
A repeat loop repeats the statements assigned to it until a following condition is true.
x = 10
repeat
print("The value is less than 100")
x = x + 10
until(x > 100)
Skip to main content Unlike a while loop, it runs the statements first before checking if the condition is true.
Break
Writing break at the end of a loop will end the loop if a condition is true. In the example below, break
is used to break out of an infinite loop that otherwise keeps running because it is always true.
x = 10
while true do
print("The value is less than 100")
x = x + 10
if x == 100 then
break
end
end
This could also work if instead of true we wrote while x < 200 do. The result would be that the loop
would end early, ending when x reaches 100 rather than 200.
You may also hear the term nested loop, which is used to describe a loop with another loop inside
of it.
Tables
Tables are used for storing large sets of data that you might use while creating your script. With
tables, you can access and manipulate the data easy, which can come in handy later on. In this
section, you’ll be learning about just a single type of tables, arrays.
Arrays
Arrays are simply a list of values compiled in a table. This can include strings, numbers, booleans,
functions–pretty much anything that can be used outside the script. To make an array, start by
assigning a name of your own to the table.
randomArray
After this first step, you need to assign the data to the table. Assign the table to the data by adding
an “=” in between, and write a pair of curly brackets (the second bracket will be automatically typed
for you).
randomArray = {}
Next, you can place all of your values inside of the array. For this tutorial, we’ll place a few strings
and numbers together, but you can add more variety to your table if you’re following along. Make
sure to separate each value by adding commas in-between them.
If we want to print specific parts of our array, we can use the index to get what we want. Each value
in our array has an index, or number assigned to it. The first value is 1, second value is 2, third value
is 3, etc. To print a specific part of our array, we put our table name in the print function, then follow
it up with the index of the value, in-between brackets ([]).
print(randomArray[1])
In the end, this should print out our first string, “Hello World” in the output.
Q&A
Where Else Can I Learn About Scripting?
Websites
Youtubers
AlvinBlox 474
TheDevKing 358
Roblox (Scripting Playlist) 994
PeasFactory 117
SkeleDotLua 171
There are many other places to learn, but the sources listed above have helped me with learning or
adapting to Lua.
Stopping exploiters isn’t a beginner issue, it is a little more advanced than what we’ve discussed so
far. Throughout your scripting career, you’ll realize that the main reason for exploits is due to poor
and weak scripts that hackers are able to bypass.
One feature that Roblox automatically now applies to all games is FilteringEnabled, which prevents
hackers from messing with the server itself. To learn more about how it works, you can watch the
following video by AlvinBlox,
Technically, indenting is not necessary, but it is extremely helpful and keeps your code organized.
For example, through indents, you can see where your code blocks are. Not only that, but it looks
much cleaner. Writing without proper indentation would give both you and the programmers
reviewing your code a headache to read.
Practice, practice makes perfect, no exceptions. Learning to program unique things by yourself is
always something you should strive to achieve as a developer.
Supersaiyan122:
When you can’t figure it out, the first step should not to be ask for help. Go to the dev wiki, go to
stackoverflow, go to the dev forums, read past posts and figure it out on your own. You could
ask for help over and over again and make a pretty decent game, but you’d never be able to
replicate that without those people around you, you won’t learn anything. It’s not wrong to ask
for advice sometimes, but it should never be the first thing you go to.
Conclusion
That’s it, the basic tutorial is now finally finished! Hopefully, now you’ll have a far easier time being
able to understand new concepts and can use this as a reference whenever you feel stuck.
If you have any other questions or feedback, please reply in the comments below, good luck!
1 Reply 347
5 more
This is an amazing tutorial. However, I still find learning LUA complex. I want to be able to build stuff
like modules and other things, and don’t know how to do so. What would you recommend?
2 Replies 26
@Anthony2003Epic I’d recommend the Studio+ plugin, it brought me far in developing and scripting.
1 Reply 33
Thank you, I factor my knowledge of scripting thanks to YouTubers and education programs like
KhanAcademy which unfortunately only teaches Java.
If you want to get a little more experienced with Roblox Lua, I recommend you watch several
YouTubers, AlvinBlox 740 is perhaps one of the best and taught me many scripting basics including
modules. PeasFactory 105 is respectable, however his content and tutorials are fairly out-dated yet
still reliable for the most part, I also recommend SkeleDotLua 348 who is very practical and focuses
on making things rather than precisely learning them, both make excellent tutorials.
Besides those channels, there are plugins such as the one Ithurius suggested. Experiment, explore,
and browse your ideas, the farther you’ll go the better you’ll get, hope this helps!
27
Can you explain what .sort and .concat means? I find tables confusing.
11
@VegetationBush
table.concat()
This function will concatenate all of the given table’s contents if they’re numbers and strings.
ie.
local array = {"Hello ! ", "I have ", 5, " blocks of cheese!"}
print(table.concat(array)) --Hello! I have 5 blocks of cheese!
table.sort()
This function will sort your table defaultly in descending order.
ie.
You can also give a function to sort by, this function is given 2 values of the table.
The normal function works like:
function(a,b)
return a < b --boolean
end
If all of this was explained on the main thread then it’d help others!
FYI: you can read more here:
@DarkSinisterPVP Awesome tutorial! Explains in a short and sweet manner of the some of the
basics, though I’d think it could be improved even more if you explained scopes and the variable
defining keyword - local - but also explaining loops such as while, repeat and the numerical for
(generic for if you feel like it too!).
Alright, updated, I added three extra topics, thank you for the suggestion!
11
Great tutorial, very useful and I made pretty much a quick guide for myself using this on my
“throwaway” place for development. Thank you.
29 days later
You forgot to mention that we should be using the Developer Hub. It’s better from DevForum or
youtube and explains everything.
1 Reply 8
3 months later
I’m glad this was helpful. I’ll still be answering questions, even now.
2 months later
Thank you for the suggestion. I’ve decided to post all viable and helpful websites and Youtubers
who’ve personally helped me learn to script. You should see it as the first question answered in the
Q&A section.
I will be coming with a huge update later on because I want to keep things a lot more clear.
Very helpful and Useful. AlvinBlox is a respectable one for sure. Though I feel his content is going
over specific things now, so that’s why I’m distancing away from him (don’t get me wrong he’s pretty
good!).
It’s also great to not only learn Lua, but to also learn other languages since some of them are similar
and can help you to your learning of Lua. Though that is an extra step, which I wouldn’t recommend
people taking when starting!
1 Reply 10
Agreed, Lua is an excellent place to start learning programming from. It has a lot of possibilities and
is quite easy to learn compared to other languages.
I hope that in the future, there will be a lot more tutorials made on both YouTube and the Developer
Forum that help find more talented programmers in different ways.
Apart from that, you might want to fix your indent issue, and there’s actually more than two types
of loops, eg Repeat loop and possibly a recursive function.
Good luck!
1 Reply 5
Yeah, I’ve noticed that a lot of more topics could be added and focused on along with clarifications
and I will make sure to follow your advice.
The original tutorial was written in a different style for the scripts so it’s been a little difficult fixing
the indents, I’m trying to update it to proper format.
I suggest checking out Talkative Entertainment 370 on YouTube. We’re activly released tutorials for
developers who have “graduated” from “beginner” to the “advanced” state.
8 months later
thank you, I watched all the basic tutorials on the alvinblox channel and I need to practice my
knowledge of scripting to be able to advance to a higher level
1 month later
DarkSinisterPVP:
Variables can store anything, not just these. For a beginner, yes they can store them, but I feel like
you should have named it as a “shortcut” since it points to info, I understand this is a beginner’s
tutorial, just felt like pointing that out.
9 months later
Do you have any specific videos of PeasFactory that are the most out-dated? I have personally tried
AlvinBlox multiple times but I find his videos kinda “meh” to understand for me.
PeasFactory seem better in my opinion, may look at some of AlvinBlox’s more advanced videos in
the futute when I’m more experienced.
Sorry for my bad grammar.
I’ve ran through most of the beginner series with no issues, it’s still based off of Lua 5.1, meaning you
shouldn’t have issues. Although, I’ve encountered some minor things that seem outdated but
nothing too major.
4 months later
hi, but does studio+ still work? It hasn’t been updated for a while now.
1 Reply
6 months later
Here’s a plug-in since CkStudio plus is down and will be down due to ransom. But there is an
alternative:
roblox.com
Reply
Suggested Topics
Want to read more? Browse other topics in Community Tutorials or view latest topics.
Privacy Policy
Parents