Lua Scripting Starter Guide
Explained by @DarkSinisterPVP With Help From @Supersaiyan122
Note: I will be updating this post in several weeks from now. When I posted this tutorial, I did not expect that this many people would be learning from it. The update should add much more information, advice, fixes of mistakes I’ve made describing things, overall a huge general improvement!
In the following tutorial, you’ll be learning on the basics and best methods to use and experiment with Roblox Studio. This tutorial is not made for professionals, this is for pure beginners or just intermediates who want to boost their skills up a bit.
This tutorial will work with steps, I believe each section is timed appropriately. If you have any disagreements or things you wish to add on the tutorial please place it down below.
Unlike building and UI design, scripting is fairly practical and has not much " tastes " to it. The goal of scripting is simple, your attempting to make the game itself work, not much else if you put it into a broad spectrum. Note, the following tutorial is for Roblox Lua, your experiences with other programs such as JavaScript, Python, C+, PHP, and all others will be significantly different.
The Basics of Roblox Lua
Proper Format
Before starting, please make sure your format is correct to ensure the perfect programming experience. Being disorganized is one of the worst things you can do while scripting, it is best to follow a specific format, but feel free to change it however you wish.
To select the proper format, click on View in the upper side of your screen, then make sure you have selected or have the following, " Properties, Explorer, Command Bar, and Output. "
Finally, your screen should look something similar to this,
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 appears as a blue scroll, you should see a white screen once finished.
Now that you have a proper format, you will learn one of the most basic commands in Lua. Printing is perhaps the first thing that all programmers learn, print is a command that allows you to print out a statement.
Erase the following text, once finished type out print in all lowercase letters, if spelled incorrectly or typed with any capitals, the script ceases to function. Then, type out (“YellowNoobs!”) after the message, make sure there is no spaces between print and ( if you want your script to work.
print("YellowNoobs!")
You may replace YellowNoobs with any other text you wish as long as it is in-between (""), or the strings, the end result should look something like this,
Congrats! You’ve ( probably ) made your first line of script! If you’d like to see if the script works, scroll down to Output and paste your command onto the Command Bar, you should see the printed text inside Output.
Variables
Now that you’ve learned how to make a script, you’ll learn what variables are and how to make on of your own. Variables aren’t necessary, but they are highly recommended and considered vital because they save a lot of time and keep your scripts organized. They store booleans, numbers, and strings.
I will start off by using a string variable, I will use the same phrase we used in the previous lesson, but instead shortening it to the word variable.
variable = "YellowNoobs!"
Variable names can use letters, numbers, and underscores, but cannot use any spaces. The starting letter in the variable should not be capital letter, and can never include a number, however underscores are fine. Let’s now try to print our string using a variable. Copy and paste your code inside of the command bar and look at the output to see if it works. Make sure to never put strings around the variable itself when printing, otherwise it will print the word “variable” rather than “YellowNoobs!”
variable = "YellowNoobs!"
print(variable)
Our string variable is a success. Let’s now move on to a number variable, we’ll assign a long math equation to a variable.
variable = 10 + 2 + 5 + 24 - 10 - 15 + 10
print(variable)
Let’s put the code in the command bar and check the output to make sure it works.
As you can see, Lua automatically solves this equation and shortens it to 26, just for fun, let’s see if the equation is correct.
It’s a success, our number variable is accurate and works!
Functions
Functions listen to events and perform a specific task, while learning about functions you’ll also hear about Arguments and Events which you’ll become experienced with later.
Below is an example of a regular Function,
function Myfunction()
workspace.Baseplate.BrickColor =BrickColor.new ("Pine Cone")
end
myFunction()
This function will turn the base-plate into a pine-cone woody color.
Local Variables and Code Blocks
Putting local before a Variable will transform it into a local variable. It can only be used in a limited scope, meaning that can only be used in the code block/function. It is good practice to use local variables because they are accessed faster than global variables.
In order to make a local variable, all you have to do is place local before writing down your variable. To make a local variable work, is a little more complicated than that, I’ve included an example of both how a code block and local variable work.
local function thecodeblock()
local variable = "hello!"
print(variable)
end
thecodeblock()
print(variable) -- Invalid, as the variable is outside of the codeblock function.
Sections and Textlines
Sections are a simple green text that organizes and names your lines of code, while also not necessary, it is highly recommended to save you hours of searching.
Creating sections is extremely easy, simply add - - without spaces and then your text after you are finished, it should appear something like this,
Getting Inside Settings
You might’ve noticed that when you do a script such as transparency = 1, nothing will happen, this is because the Roblox Engine has no idea what to make transparent, you need to give it more data for it to actually make the base-plate transparent,
game.WorkSpace.Baseplate.Transparency = 1
Now, your script will work because the Engine has enough information to execute a command, making the base-plate transparent.
Local Scripts
Local Scripts can execute commands only with the LocalPlayer inside of it, a regular Script would never work in a Local Script. Basically, it runs the player’s client instead of running the server like a normal script would do.
Here is an example of a LocalScript,
tool = script.Parent
player = game.Players.LocalPlayer
tool.Activated.connect(function()
player.Character.Torso.Transparency = 1
end)
This will complete make the torso invisible, also, notice how I used LocalPlayer?
Booleans
You might’ve heard this word in math class and it is related. Booleans in scripting are either true or false, when comparing values you’ll get a Boolean.
1 > 4
2 == 5
8 < 2
Intermediate Lua
Repeat and Loops
There are four types of loops, all are useful for your games, they are called for loops, while loops , repeat loops, and nested loops.
Below, is an example of a For Loop,
i = 10 + 5
if i < 30 then
print (i)
end
wait(1)
Below is an example of a While Loop,
while 2+2==4 do
local part = Instance.new("Part", game.Workspace)
wait(5)
end
Statements
This is perhaps one of the most important topics in programming/scripting, it makes it so that specific commands only happen if the criteria is met, a statement matches the script below,
if true then
print("Congratulations")
end
For example, if 5 > 1 then 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, you’re also using Booleans!
Tables
While not the most beginner topic, tables are frequently needed for games. Tables come in all forms, typically being an Array or Dictionary and even a Mixed-Table which are all helpful for services in your script/game.
Tables are basically lists, here is an example of a table,
coolTable = {1,2,7,9,3}
table.sort(coolTable)
print(table.concat(coolTable))
Q&A
Where Else Can I Learn About Scripting?
Websites
Khan Academy (JavaScript)
Stack Overflow
Roblox Developer Hub
Lua.org
Google
Youtubers
AlvinBlox
TheDevKing
Roblox (Scripting Playlist)
PeasFactory (Outdated)
SkeleDotLua (Outdated)
There are many other places to learn, but the sources listed above have helped me with learning or adapting to Lua.
What is the Difference Between Input and Output?
Input is the command given to the engine, while Output is the received/finished command, for example, print(“YellowNoobs!”) is a Input while the result in the Output below is the output of the command.
How do I Stop Exploiters?
Stopping exploiters isn’t a beginner issue, it is a little more advanced than interpreted. Throughout your scripting career, you’ll realize that the main reason for exploits is due to poor and weak scripts. In order to stop exploiters, I recommend using FilteringEnabled, AlvinBlox describes this greatly in his video.
Why does Studio Automatically Indent for me?
Indenting script is purely a matter of choice, however it keeps scripts organized and far easier to use than a one line mess. In various languages such as Python, it is required that you indent certain lines of code in order for it to be able to be read by the compiler.
How do I become better?
Practice, practice makes perfect, no exceptions. Learning to program unique things by yourself is always something you should strive to achieve as a developer.
That’s it, the basic tutorial is now finally finished! As reviewed, you’ve learnt the basics for scripting and should be able to make a simple command on your own.
If you have any other questions or feedback, please reply in the comments below, good luck!