Lua Scripting Starter Guide

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.

Print

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.

HelloWorld

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,

YellowNoobs!

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.

PrintingOutput
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.

image

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.

image

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,

image

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

95 Likes

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?

5 Likes

@Anthony2003Epic I’d recommend the Studio+ plugin, it brought me far in developing and scripting.

11 Likes

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 is perhaps one of the best and taught me many scripting basics including modules. PeasFactory is respectable, however his content and tutorials are fairly out-dated yet still reliable for the most part, I also recommend SkeleDotLua 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! :smiley:

8 Likes

Can you explain what .sort and .concat means? I find tables confusing.

2 Likes

@TOP_Crundee123
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!

You can also give serperator:

local array = {1,2,1,2,3,4, ""}
print(table.concat(array, "! ")) --1! 2! 1! 2! 3! 4! 

table.sort()
This function will sort your table defaultly in descending order.
ie.

local array = {5,3,4,1,2}
print( unpack(table.sort(array)) ) --5 4 3 2 1

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 we wanted to sort by ascending order, you’d do:

local array = {5,3,4,1,2}
print( unpack(table.sort(array, function(a,b)
    return a > b
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!).

Nonetheless, great work! Keep it up dude.

8 Likes

Alright, updated, I added three extra topics, thank you for the suggestion!

4 Likes

Great tutorial, very useful and I made pretty much a quick guide for myself using this on my “throwaway” place for development. Thank you.

3 Likes

You forgot to mention that we should be using the Developer Hub. It’s better from DevForum or youtube and explains everything.

1 Like

I’m glad this was helpful. I’ll still be answering questions, even now.

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!

2 Likes

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.

1 Like

image
PS: Please teach your reader to use game:GetService(string Service)

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!

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.

Thank you for the suggestions!

I suggest checking out Talkative Entertainment on YouTube. We’re activly released tutorials for developers who have “graduated” from “beginner” to the “advanced” state.