What are considered the basics of scripting?

Good morning,

I’ve tried to use the reverse engineering method of using free models to learn scripting ;however, I always seem to get lost onto what the script was trying to do. I’m aware that proper basic knowledge of scripting is needed to proceed with the reverse engineering method, but frankly I have no idea what is considered “basic scripting knowledge”. I’ve tried going onto the new dev hub to look up info but I couldn’t understand it either. I wanna know the what’s considered the basics of Roblox Lua so that I proceed with learning from free models. Thank you for your time.

2 Likes

If statements, functions, loops, variables, and the Roblox instance hierarchy. For the last one just open the Roblox browser.

Basic scripting knowledge is what you should learn first. It includes:
1: For loops
2: While loops
3: If statements
4: Variables
5: Functions
And many more.

Variables, functions, events and loops.

I believe learning the basics of programming through Lua before delving into Luau would serve as a more productive way to learn coding, especially since your knowledge would be derived from more generic content which allows you to create more.

I’m not sure as to how much you know about lua or programming, so I’ll start from the beginning so anybody may understand. At its roots, programming is made up of three basic constructs: Sequencing, selection and iteration - all programs and scripts come from these. As far as what they do, it’s pretty self explanatory within their name, but I’ll still go over each one. Once I’ve done that, I’ll explain more basic concepts such as the variable, the function and more.

Sequencing is as simple as it gets with programming and is just the order your instructions (or otherwise code) follows. Typically in programs, developers will have their scripts split into three sections: The declarations, functions and main program. This looks like the following:

-- Variables
local heightLimit = 200
local password = "coding"

local personsHeight = 195
local password = "coding club"

-- Functions
local function CheckEntryAllowance()
    if (personsHeight < heightLimit) and (passwordEntered == password) then 
     print("Welcome in brother")
   else
     print("You're not allowed in")
   end
end

-- Main program
CheckEntryAllowance()

Don’t worry about understanding each individual line here as that isn’t the main point of the screenshot. Instead, take note of how I have laid out my variables at the top, the functions (which handles the main bulk of my code and my instructions), and finally the main program. This is how most programs will be laid out, so is the overall sequence, but keep in mind sequencing still applies to the level of each instruction. If we decided to “declare” our variables after we use them, we’ll get an error since our code needs to know what it’s working with before it does anything to it.

Next is selection, and here is where we’ll introduce our first bit of code. With sequencing, we can run any number of instructions in a row, however we face an issue: if our code is dependant on certain conditions, such as to only run if the weather is below 15 degrees celcius, we are unable to do this. This is where our IF statement comes in, and looks like the following.

if (CONDITION) then
   -- Instructions are here
end

Here, our IF statement is split into three parts. First, our keywords if, then, end, our Condition and finally the instructions. Whenever we make an if statement, we must use all of these parts in conjunction. The keywords cannot change, i.e you can’t say sometimes (condition) then. However, our Condition and the Instructions may be whatever we want.

I’ll first focus on the condition (note that it does not need to be inside of parenthesis - it simply helps distinguish it from the keywords beside it). This condition is a true or false statement which says whether we should run the instructions between it and the end keyword. In programming, a true or false value is referred to as a BOOLEAN.; this will be discussed more within our variables section. When we write our conditions, we have a few symbols we can use: I shall discuss ==, > and <.

The first symbol is ==, and this is used to represent if a value is equal to another value. For example, (money == 7) would be true if my money variable is equal to 7. If it’s not, then it’ll be false. This symbol can also be applied to different data types (discussed in variables), such as: (name == "Ben").

The following two symbols serve the same job, but do opposite things - the > and <. These operators (symbols) compare two values and will return true if the first value is larger than the other and vice versa. We can only use these operators for numbers, as “Ben” cannot be bigger than “Seventeen”. An example of us using this is as such: (25 > 10), which would be true, or (moneyInMyAccount < 0), which would once again be true.

Keep in mind we can use variables for both sides of our condition, so we can do expressions such as (player1Lives > player2Lives), or (benScore == maxScore). If we wanted to check if something is not equal to the other, we would use the sign ~= and apply it as such. (benScore ~= maxScore)

Now we have the ability to make IF statements and use selection within our programs, why don’t we try creating something. We want our code to run if the name is equal to billy, the age is equal to 23 and his height is greater than 180 (cm). Putting that all together, we get this:

if (name == "Billy") then
    if (age == 23) then
        if (height > 180) then
            -- good job, its our guy
        end
    end    
end

Notice how we used IF statements within our prior IF statements to check another condition. All though it worked here, if we have hundreds of conditions which we need to check, our code is going to go very far to the right and be very messy to work with. Some would say the solution would be to just get rid of the indents (or otherwise spaces), but that’s a messy substitute. Instead, let’s introduce some more keywords.

So far, we’ve only dealt with our IF statements to take in one condition, however we can manipulate our IF statements to deal with more than one condition through the use of our and, or keywords. When we use and, we must see if two statements are true, and if they are it will be true. If one statement is true and the other is false, then the overall statement is false. The same is true if both are false; the overall statement will once again be false. This can be applied as such to our previous case:

if (name == "Billy") and (age == 23) and (height > 180) then
    -- Instructions
end

Keep in mind we are not really adding more conditions to our IF statement: our overall IF condition is still one. However, we use our and keyword almost like a glue, to join these conditions together. It’s better seen if you add one big parenthesis around the entire statement, as such:

((name == "Billy") and (age == 23) and (height > 180))

Next is our or keyword, and instead of checking to see if both condition1 and condition2 are true, it will be true if our first condition OR our second condition is true. Lets say we want our heating to turn on if the temperature is below 15 degrees, or the button is pressed by the person. We can’t use an and keyword here, as that would require the temperature to be below 15 degrees and have the person press it. Instead, our code will look like this:

if ((temperature < 15) or (buttonPressed == true)) then
    -- turn on the heating
end

We can actually shorten this code a little within our 'second 'condition, (quotes around second since it’s not really a seperate condition as mentioned earlier), as whenever we have a variable equalling true, we can just write the variable. For example, this would make our condition into this:

((temperature < 15) or (buttonPressed)) 

If you don’t feel comfortable writing it like this, feel free to keep saying == true or == false; it doesn’t change how the code works, its just easier.

Finally for our selection, if we have an IF statement, we can use another keyword known as else to run code if our if statement does not equal to true. This is extremely useful in cases where we still want something to happen, even if our condition is evaluated to be false. It looks like the following:

if (condition) then
    -- Instructions for when the condition is true
else
    -- Instructions for when the condition is false
end

I believe examples are always more helpful than just seeing generic code, so lets say we have a game and we want to see if player 1 or player 2 wins. Instead of dealing with the case where they get the same score, lets assume that if player 1 doesn’t have more points than player 2, then player 1 loses.

Our code would then look like this:

if (player1Score > player2Score) then
    -- make player one win
else
    -- make player two win
end

One final note before I move on is that we can use elseif to test other conditions if our first one isn’t true, however you can learn about that within your own time. This also goes for the not keyword, which is useful but not mandatory to know about.


Finally, our last critical building block is iteration. With sequencing and selection, we can run whatever code we want. However, a problem emerges when we want to run our code a certain number of times. All though it would be easy to just copy and paste our code and call it a day, if we do not know how many times it needs to run, we may be in for some trouble.

Imagine your program allows your roblox character to move three blocks forward. If you wanted to move forward six times, you’d just have to run the program six times. However, if you wanted to move forward six hundred times, you’d have to run the program six hundred times. This may get tedious and annoying, so we will use iteration to fix it. Iteration simply means to repeat a process, and there are only 3 types of iteration in lua.

When dealing with iteration, we use loops to repeatedly run our code for as many times as we’d like, and the different types of loops correspond to different needs. Starting at the simplest, lets go over for loops. These are nice for running our code a specific number of times, which is told to the computer before it enters the loop. As we know for how long the loop will last, it is known as definite iteration, and is shown as follows:

for i = 1, 10 do
    -- Instructions
end

I would recommend reading about variables before continuing within our loops. Now equipped with the knowledge of variables, you’ll seem to notice it’s a bit strange to have a variable be made in such an odd way, in the middle of a loop. However, this variable holds a very specific and important job. It is known as the index and is labelled with an i simply because programmers are too lazy to write the whole word out. This number says how many times the loop has been ran.

Next to the =, there are two numbers. The first number, which is the 1 in this case, says what number i will start from. The second number, which is 10, represents the number the loop will go up to before stopping. Finally, we have our keywords for, do, end.

Lets use our loop to do some counting and see why it’s vital for iteration to exist within our programs. Below this is two examples of code, both which use the print function to write to the output the numbers 1 through 10.

Program 1:

print(1)
print(2)
print(3)
print(4)
print(5)
print(6)
print(7)
print(8)
print(9)
print(10)

Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Program 2:

for i = 1, 10 do
    print(i)
end

Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Can you see how program 1, despite only using 10 lines of code, gets the same output as program 2, which only uses 3? Imagine if I had the time to go to 100, or 1000. It would take me ages to write out the code in program 1, however would only require me to change the 10 in our second program to 1000. This is pretty incredible and saves us a lot of time.

Before I move onto some other types of loops, it is important to mention that we can once again use variables to represent our starting number and ending number. Feel free to experiment around, seeing what works and how using variables may affect the amount of times the code loops.

Now, we have discussed definite iteration, which to remind you is just the process of looping our code a set number of times, but lets talk about indefinite iteration. This is the opposite of definite, meaning we do not know how long our code will be looping for before it stops; it could be for 20 iterations, it could be 2000. All though this does not seem useful at first, often our code needs to have the ability to run forever, otherwise it may stop before it’s fully done.

Imagine you have a program to move your car forward while there is no objects in front of it, and you want your car to drive for as long as possible before bumping into something. If we used a for loop, it may look something like this:

for i = 1, 10000 do
    -- drive car forward
    if (objectInFrontOfCar == true) then
        break
    end
end

(I never talked about the keyword break, however all it does is exit the loop and stops it from continuing.)

This code is flawed as we assume that there will be an object in front of us within the next 10,000 moves. If there is, we’re fine and we will stop. However, if there is no object in front of us after 10,000 iterations, we will find ourselves stopping anyway as we have only told our loop to go for 10,000 times.

We can fix this by using a while loop. Unlike our for loops, these implementations of iteration will go until our condition inside of it is false. The generic form of one looks as such:

while (condition) do
    -- Instructions
end

Now we’ve got a loop which can go forever, and works on our previous principles of selection - how cool. With great power comes great responsibility, as if we mess up the condition for this type of loop, our code may be doomed to loop forever. Why don’t we see how our car would look inside a loop like this, instead of a for loop:

while (noObjectInFrontOfCar == true) do
    -- Drive the car forward
end

That is a lot simpler, uses less lines of code and most importantly works as intended.

One final note for iteration is that another loop exists, known as the repeat until, however basically operates in the same way as the while loop. If you would like, research it yourself and play around with it.


Variables are the next topic and will be one of the most prominent concepts in programming that you’ll ever use. Whether you’ve heard of them or not, you’ve definitely seen them, and may have even used them without your knowledge. Variables are simply names which store certain types of data or information, and are declared as such:

local variableName = data

(veteran programmers will say your variables do not need the local before it, however it is a good practice to do so and you will find out why later in your scripting journey.)

When we give our variables names, we can call them almost anything, apart from keywords which are reserved for the program. For example, we can’t name our variables local, and, if, etc. In addition to this, they may not have any spaces within them, and must not start with a number. This would mean all of the examples below would cause an error:

local 123easyas = "ABC"
local first name = "Ben"
local end = "end"

we can use variables for numerous things, and I’ll give you just a few examples below:

local name = "Ben"
local age  = 23
local amIRichYet = False

Notice how there is a different colour associated with all of the data within our variables when we declare them - this is intentional. This is because all of the variables I just created have different data types, which is basically the way different types of data is stored. This is important to know about as the way our data is stored changes the way we can use it.

I’ll first start by going over the basic data types in lua, which are the following:
string, number, boolean.

A string is any text we store, and when we store data as a string, we have to tell the program it is a string by wrapping it inside of quotation marks, e.g. "ben" or "twenty one". We can store any character (anything that can by typed on a keyboard) as a string, so we must be careful when storing numbers for reasons I will get onto later. However, for now you should know we can store numbers as strings like such: "212".

The next data type I mentioned is a number, and that represents numbers such as 23, to decimal numbers such as 14.52 . In reality, these two types of numbers are known as “floats” and “integers”, however the difference is not important at this stage. With numbers, we can do any maths to them like multiplying, adding and subtracting without any issues. For example, local result = 10 - 5 would return 5. If we attempted to subtract strings from eachother, we’ll get an error. Going back to our previous example, local result = "10" - "5" would not give us "5" as a result, but rather the program would fail to run as it cannot subtract a string from another string; it’s like subtracting the name billy from joe.

Finally, our last data type is a boolean, which just means true or false. This can be very useful for when we only want our variable to be one of two answers, e.g. local temperatureIsBelowZero = False. When declaring (making) our variable, we do not need to use quotation marks around the words False and True as they are keywords recognised by the program. This once again means we have to be careful when making variables, as if you want somebodies name to be called False and forget the quotation marks, you won’t see any errors to begin with, but attempting to use it in conditions later on will cause issues. For example,

local name = "Ben"
local enemyName = False
if (enemy == name) then
    -- Start awesome fight scene
end

Our code here will not work, or should not work, as we are trying to compare a boolean value to a string value.

This brings me to my final point, which is you cannot compare different data types of values as it just doesn’t make sense. The string value “Ben” can’t be bigger or smaller than the number 18 as “Ben” is not a number. The string value “False” cannot be equal or unequal to the boolean value False as one is a definitive no whilst the other is just the name of it.

Finally, once we’ve declared our variables, we will not need to use local again to change it’s value. For example,

local name = "Ben"
name = "Thomas"

Functions are the final thing I will cover within this giant post, mainly because this has taken a very long time to write out. These will be the most advanced things I cover within this post, so don’t worry if you don’t understand them immediately.

When we write our code, sometimes we’ll find ourselves doing the same lines of code over and over again. For example, if we were writing out multiplication of two numbers, we would have to keep copy and pasting our same mulitplication code, like the following:

local number1 = 5
local number2 = 10
local result = 0
for i = 1, number2 do
    result = result + number1
end
print(result)

number1 = 6
number2 = 12
for i = 1, number2 do
    result = result + number1
end
print(result)

Output: 50, 72

This is a simple way to multiply numbers together, however each time we wanted to multiply, we had to rewrite our code, taking multiple lines. If we wanted to multiply 100s of numbers, this would take thousands of lines. So what’s the solution?

It’s time to use some functions. A function is a section of code which does not run until we tell it to (and when we want it to run, we do something which is known as calling the function). These can store any amount of code and will run just like if it was sequenced normally. So, how do we make a function?

The generic creation of a function is as such:

function FunctionName(parameters) -- do not worry about parameters
    -- Instructions
end  

Now, whenever we want to call our function, we simply need to just write FunctionName().

Lets see what this looks like with our multiplication code.

local number1 = 5
local number2 = 10

local function Multiply()
    local result = 0
    for i = 1, number2 do
        result = result + number1
    end
    print(result)
end

Multiply()

number1 = 6
number2 = 12
Multiply()

That is a lot simpler than needing to copy and paste our code several times over! Notice how we created a variable inside of our function - if we tried to access it outside of its scope, i.e. outside of the function it was made in, we would get an error. That is because now our result is made within the function, it can only be accessed from within the function.

If we wanted our code to be extra clean, we could replace number1 and number2 within our function with parameters and pass our numbers in as arguments, however that is too much for an introduction to functions.


Closing remarks:

I would say that all though I’ve covered a decent amount, there will always be more to learn. Programming encapsulates so many unique and interesting ideas that it would take me years to truly understand and explain in a presentable way every niche and minute detail about it. For now, I would recommend reading this again if you didn’t understand certain topics, and if you are confident with it all, to follow through into some of the topics listed below:


GENERIC CONCEPTS FOR PROGRAMMING:

  • Repeat Until loops
  • Parameters
  • Elseif Statements
  • Not Conditions
  • Arrays
  • Dictionaries
  • for i in pairs loops

ROBLOX PROGRAMMING

  • Vectors and CFrames
  • Properties
  • PlayerService
  • UserInputService and ContextActionService
4 Likes

Tysm for the intensive response. I’m a beginner at scripting so this will help.

1 Like

I noticed that you started typing 6 mins after I posted. I appreciate you taking time to answer me.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.