Guide on the basics of scripting

Hi all. This is will be an extremely long topic on the basics of scripting. I have recently just finished learning scripting(the basics), and some of the topics were really confusing. After rewatching the videos over and over again, I can now say that I have a very basic knowledge of scripting.
Three things before I start the tutorial:

  1. Here are the links to the playlists I watched, I would highly recommend watching and writing down notes to get a better understanding
    Number 1
    Number 2
  2. If you guys want a video version of this, just drop me a dm. Do note that I can’t record my computer screen because it isn’t very good.
  3. This tutorial requires a basic knowledge of studio and how it works, if you don’t know that, I suggest you watch the first videos in the playlists.(Sorry for that)

Alright Let’s Start!

NOTE: To get your script to play or the code to run you have to click on the arrow right below Play and click Run. Running is basically doing what the script or code does without having to play the game. When you press Play, it will be as if you’re in a Roblox game and playing your own game. If you have a spawn area, it will spawn you there, otherwise, it will randomly spawn anywhere around your map. Play here is like play but you spawn wherever your camera is facing.

Properties

When you first get into studio, you should be on the page called “home”. You can then insert a part by clicking on part at the top.


It should look something like the picture above. Next you want to go into game explorer(Click view on the top and then explorer) and hover your mouse above “ServerScriptService” and a plus icon should appear. Click on the plus icon and a bunch of things in a UI will appear but you have to find one called “script”. You can also search it in the search bar.
Once in the script, it should say something like print(“Hello World”). Just delete that.

Now, we can start changing the properties.

game.Workspace

You may be wondering, what’s game.Workspace. Well, game is simple enough. Game is what stores everything. Like the workspace or the Players(see the game explorer) or Lighting etc. Workspace is what stores all your parts. Like a part in the Workspace. Kinda like a descendant of game. When you do game.Workspace, this basically calls game and workspace(not literally). It’s telling game and Workspace that you’re about to change something. Note: When you put a dot, it’s just referencing the things in the game. You HAVE to do game.Workspace because you need to call the game and workspace.
Next, if you click on the arrow on the left side where it says Workspace in the Explorer window, you will see that “Part” is in it.


That’s because Part is a child of the Workspace(that’s what it’s called). So, as mentioned earlier, you have to call Part. So you do

game.Workspace.Part

Now, we can start changing the properties. Okay, you may be wondering what is a property? Well, it’s features or things you can change within the object or Part. These properties are changeable so as to suit your needs or what you need for your game.To open up the properties windows, click on view and then
properties, it’s right next to explorer.
The properties window will be empty unless you click on something in the explorer window.
So click “Part” which you inserted earlier from the Workspace and a list of properties will appear. For me it appears on the left hand side because I placed the window there.


You can study all the properties by yourself, you can find out about them in the developer hub or object browser(I will talk about that later). I will be going through some of the more common properties and what they are.

  1. Material. This changes the texture of the part or whatever object you inserted. There are tons of options to choose from such as “Wood”, “Plastic”(This is the default material they give you when you insert the part), “Glass”, “Marble”, “Metal”, “Neon”(This is really good if you want to have nightlights in your game) etc. Like this:

  2. Transparency. This changes how opaque or transparent the object will be. You can change it from 0(opaque) to 1(transparent). Opaque is what you get when you insert a part in the workspace but transparent can’t even be seen. Even if I take a pic, there won’t be anything there and I don’t want to confuse you guys any further. You can try out and see the transparency yourselves.

  3. BrickColor(You have to use American and not British English). This changes the colour of the brick to whatever you want, be it “Really Red”, “Pearl”, “New Yeller” etc. There is also another property called Color which I won’t be going through today.

  4. Reflectance. This changes how much the object reflects(kinda obvious). 0 is no reflectance and 1 is fullly reflectant like this:


    5.Anchored. This changes whether the property will stay where it is or be able to move around. An example would be a football game. The football will have it’s anchored property turned off so that the players will be able to move the ball around(kicking). If the ball or object is anchored, then players will not be able to move it around.

  5. CanCollide. This is simple enough. It basically means whether something or someone can pass through another object. If the CanCollide property is turned on, that means if something tried passing through it, that something will be stopped or blocked because it is on. If the CanCollide property is off, you will be able to pass through the object, like this:

  6. Position. Position is a Vector3 value, meaning that you have to enter 3 digits in an ordered list. When you key in the Vector3 value, you will be setting the X,Y,Z value. What is X,Y,Z value you may ask. It’s the three axis. If you look carefully on the previous pics, you will notice a cube there. It shows the three angles/axis, x,y and z. If you open studio, it will be there by default.

  7. Name. The name says it all. It’s basically what the object or part is called. If you change the name of the part to “Keys”, then in the script, you have to type game.Workspace.Keys because there’s no more object called “part” in the workspace
    Okay there are a bunch of other topics but I won’t go through them. If you want me to cover those, you can leave a reply on this topic.
    Now, on to the script. A quick recap in case you forgot: You have to write game.Workspace.[Insert Object name] to call or reference it. Okay, now let’s use the script to change a property. How about transparency? (The preformatted text function isn’t working for some reason)

    game.Workspace.Part.Transparency = 0.5

What this does is it changes the Transparency of the Part to 0.5. If you noticed, I added .Transparency after Part because we want to call of reference that property.

game.Workspace.anchored = false

You may be wondering,“Wait, why didn’t you put it equals to a number”. Well, that’s because the anchored property is a boolean value, meaning that it can only be true or false. If you set it to false, the object will be able to be moved around. If you set it to true, the object won’t be able to move around.

game.Workspace.Part.BrickColor = BrickColor.new("New Yeller")

Again, you might be confused. Why can’t you just do game.Workspace.BrickColor = (“Really Red”).
That’s because you can only do that for number values. So when you put it as BrickColor.new you’re setting it as a new colour.

That just about covers everything about properties, I may be using some in the later tutorials so be sure to learn how to do these. Time for the next tutorial!

Printing

This is a very simple thing but it can be very useful. A lot of people however, don’t see a point in it. I will be explaining why it can be useful later on.
Okay, firstly, you need to have the output window enabled. The output window is useful in detecting what went wrong with your script. You can find the output window in view(on the top) and click output. Now let’s write our print statement.

print(“Bacons are awesome”)

What this does is that it prints a statement and puts it in the output. Print is basically like writing except for Roblox or any other programming language(I think), you have to write print first because the programme only understands it when you write it like that. If you just write is as (“Bacons are awesome”), the script won’t detect it because it was not programmed to recognise that. Which is why you have to put a print before whatever word you want to write. Here’s what it will look like:



If you can see in the output window(at the bottom) of the second picture, you will see that it says Bacons are awesome. That’s because we did the print statement and so it put the message in the output.
Some of you might be thinking(“Okay, this is quite cool but there’s no point for it”). Well, there is. You see, let’s say you have long lines of code and maybe you’re really tired that day or something and you make a mistake in your script. The output window will tell you where you went wrong but a print statement can do that too! Let me show you an example:

You can see that I put a print statement in between each game.Workspace code and that’s because I want to make sure that all my codes run.(Also, I showed you guys another example of a Vector3 value in case you’re still confused) I’m not too sure if you can see but on Line 5, I made a spelling mistake and instead of Workspace, I spelled it as Workspcae. That’s when out print statements really help. You see, each time one of the lines finish running, like game.Workspace.Part.Transparency = 0.5, a print statement will be put in the output. And if one of the lines of codes have an error, the codes after that line won’t run. So, since line 5 didn’t run, because the print statement wasn’t displayed in the output, you can narrow the error down to line 5. This is better done with a script with more lines of code but this is good enough. Here is a pic of what I mean:

You can see that it only printed (“Line 1 complete”) so you know that somewhere between line 4-6 an error happened.

Okay, this wraps up printing and let’s move on to the next topic!

Variables

What are variables? Well, put simply, they’re a way to shorten the code so that you don’t need to write the same thing over and over again. Here’s an example:


You notice how you have to keep writing game.Workspace.Part repeatedly. Well, that can be solved with a variable! Let me first give you an example of a variable. E.g.

local cheese = 5

That is a variable. Note that you have to put local. “Cheese” can be replaced by anything, it can be donuts, mouses, school whatever you want. However, you have to make sure that you know what it is for. If you put something at random and you have tons of code, you might not know what a variable is for. So, if you’re making a script for a shop you can name the variable as shop. So the script will be:

local cheese = 10

Please also note that you don’t only have to put a number after the equals sign.

Okay now, let’s get back to out first example. So, the problem here is that you keep writing game.Workspace.Part and that isn’t efficient. So, you can add a variable. Like this:


Let me explain what’s going on in the script. You are setting game.Workspace.Part to a variable called hop. That way, you don’t need to write game.Workspace.Part and can just write hop.Transparency = 0.5(for example). You can test it out for yourself in studio. The script still works perfectly fine.
Read: You have to write the variable first, you can’t write all the other lines of code first and then at the end just say

local hop = game.Workspace.Part

That won’t work because the script runs in order from line 1 to line 10, not line 10 to line 1.

Okay, we’re done with variables! Give yourself a pat on the back if you’ve made it so far.

Functions

What are functions? Functions are a block of code or lines of code which can do an action repeatedly without you having to write the code again kinda like variables.
Here’s an example:


As you can see, you have a part which will have those properties in the script. But, what if you wanted to have 3 of those bricks or parts. You could copy and paste the lines of codes but that will cost you some time and you will have tons of codes. You can solve that with a function! Here’s an example of a function:

This is something like a variable but also different. As you can see in the pic, you have to write:

local function best()

You need to write local function and the brackets after whatever you want to call your function. so it could be honey(), bee(), fish() etc. Afterwards you have to call your function. This is an important step because if you don’t call your function, the function won’t work. You can call your function simply by doing:

best()

This has to be done when you want to close off your function or at the end of the code. Okay, now let’s get back to the first example. So, you have a bunch of code, what do you do? You place them in a function! Like this:


As you can see, we have placed all the codes into one function. Now, let’s say you have 3 parts in your Workspace and you want to give these properties to all of them, what you do is:

This gives the properties to all the parts without having to write the same code again! NOTE: You have to name the three parts different things because if they all have the same name, E.g. “Part”, then the workspace doesn’t know which want to give it too and will only give the properties to one of the parts.

That’s all for functions and let’s move on to the next topic.

Instances

What is an instance? Well, with an instance, you can add things into the game without having to do so manually. You can add a part, fire, a script etc. Here’s an example of a script with an Instance:


As you can see, you need to have a variable first and in that variable you need to put

Instance.new()

Instance.new is the command to add an object to wherever you want to place it. Inside the brackets, you can put what you want inside. So if you want a part, just put (“Part”), if you want fire, then put (“Fire”). Afterwards, you need to put where the object that you want goes in. You can put it in Workspace, Players, Lighting etc. To do this, you have to tell put whatever the variable is called and put .parent afterwards. Parent is basically like the owner(in a way) of that object. If you don’t tell where you want to put it, the part won’t spawn. In this case, I put it in workspace. And as you can see, a part spawned!

That’s all for instances and now let’s move on to the next one!

Parameters and Arguements

This is a very confusing topic and when I first learnt it, I didn’t understand it. Even now, it will be difficult for me to explain this to you but I will try my best.

So, this is in relation to functions which we learnt just now. With parameters and arguements, you can do a lot more with functions. You can actually choose what properties each part has. We will also be using a bit of Instances in this tutorial.
Firstly, let me show you an example:


As you can see in the picture, there is one thing we changed from the normal function and Instance. We put in the brackets a property- Transparency. And what that does is that it allows us to be able to change the transparency of the parts that spawn, without having to do so manually(meaning that you don’t need to use the property window). If you notice on line 7, I set the transparency as transparency. Meaning that this can be filled up later, because at the start of the function, we established that transparency will be decided during the calling of the function.

At the end, when we called the function, you can see that I put decimals in the brackets. This is because now, we can change the transparency to whatever we want. So, for the first one, the transparency is 0.5, the second part’s transparency is 0.3 and the last part’s is 0.7.
You don’t have to stop just at one property, you can have multiple ones like this:

That covers it for Parameters and Arguements. If you didn’t understand it, then I’m really sorry but you can look up posts on the developer hub and watch the videos in the playlist.

Built in functions

There isn’t much teaching in this topic because this is more whether you want to explore and more self-directed learning.

So, built in functions are basically functions that Roblox has already programmed inside it’s system. Some examples are Clone(), Destroy(), BreakJoints() and more. To find out about all these built in functions, you can go to view and on the top, there will be a button called object browser, click on that and it should bring you to something that looks like this:


What is the object browser? Well, the object browser is a list of functions that you can use. There will be two sides to this object browser. The one on the left are all the objects you can insert into the game. On the right side, we have the functions of what that particular object can do. One of the most common object you will use is Part. If you look at part, you will notice that there are tons of functions there. They also have a list of the properties of Part. If you notice some with a lightning logo, those are events, which we will be covering in a tutorial later. Let me go through what some of the functions are:

  1. BreakJoints() This basically kills someone(Not irl). Like a killbrick. That’s how the killbricks in obbies are made.
  2. Clone() This clones/duplicates/makes a copy of the part.
  3. Destroy() This destroys the part/removes it from the workspace or wherever the part is in.

The rest of the functions actually have descriptions in them(Most of them do), so you can read the desc and find out what that function, event, property does.

That’s all you need to know about built in functions so let’s move on to the next topic!

Events

What are events? Events are things that are triggered by the player. So, an example would be if the player touched something, that could trigger an event and cause something to happen. Here is an example of an event in a script:


There are two parts to this script. The first one is simple enough. When you do game.Workspace.Part.Touched:Connect(), this basically makes it such that when something connects or touches the Part, the event is fired. When you do function, you are connecting the event to a function(if I’m not wrong).Now you may be wondering what is function(hit), well, that is when the part gets hit/touched. Now when you do print(hit), that prints out what touched the part. When you enter a Roblox game, you are known as a Humanoid and you have different parts, like body parts. So you have the lower left foot, the upper right foot, the head etc. So, for example, if someone touched the part with their lower right foot, then in the output window, it will display it as LowerRightFoot.

The second one is the script for a killbrick. Let’s study the script a little closer. There may be a few things that you don’t understand from the script, like the if statement which I will be covering later. All you need to know for now is that an if statement is this: If a condition is met, then something will happen. We also have the FindFirstChild function. This finds the first child of the parent. We learnt about parents earlier and so a child is a descendant of parent. An example of a child would be a Part, fire and a lot more. So, when the humanoid(you) hits(touches) the Part makes your health 0 or kills you, whichever way you see it.

We’re about halfway done now! You can do this!

If Statements

What is an If statement? We touched on this briefly in the previous topic. An if statement is: If a condition is met, then something will be done. Here’s an example of an if statement:

if 1+1 == 2 then
    print("Hello")
end

This is a very simple if statement. So, this means that if 1+1 = 2 which it does, then it will print “Hello” which it will since the condition is met. You may be wondering why I put == instead of = and that’s because if you put just one equal sign, then you’re setting a value to another value. If you put two equal signs, that means that you are checking if something is set to another value.

This is a simple intro to if statements but it should be easier to understand.

Elseif statements and embedded if statements/nested if statements

What is an elseif statement? Well, we learnt about if statements in the previous topic and elseif statements is something that goes with it. I’ll explain with this example: (I can’t post anymore photos because my topic is too long)

if 1+5 == 10 then
 print("Yes")
elseif 1+5 == 7 then
 print("Maybe")
else
  print("No")
end

So, as you can see, elseif is like another if statement, it extends the if statement so that there will be more alternatives. Not only that, you can see the else statement. The else statement is if none of the if and elseif statements(you can have multiple elseif statements) work, then it will do whatever is in the else statement.

What is an embedded if statement(or a nested if statement)? Well, an embedded if statement is an if statement within an if statement or multiple if statements within each other. Here’s an example:


As you can see, this will print(“It did work”) because paint(7) + 5 = 12.

That’s all for this topic. Let’s move on to the next one.

While and Repeat loops

What are while loops?A while loop basically runs certain lines of codes while something remains true. An example would be this:

This while loop will keep running the code print(“Yes, it worked”) because 1+1 will always be equals to 2. If you want the loop to run, you must put a true value in between while and do.
NOTE: You need to put a wait() in the while loop statement. What a wait() does is it waits for a certain amount of time before running the code. You can put it as wait(10) and it will wait 10 seconds to run the loop. If you don’t put a number in between the brackets, it will just wait for the lowest possible time.

What is a repeat loop? A repeat loop is similar to a while loop. It will keep running a certain block of code until something happens to stop the loop. Here’s an example:

This loop will keep running until rnum’s value is = or greater than 10. You would have noticed the rnum = rnum + 1 on line 6. What that does is it adds 1 to the value of rnum every time the loop runs. So, if you run this script, it will print(“Why are you doing this”) 5 times before the loop stops because the condition has been fulfilled.

We have two to three more topics to go so just hang in there!

Tables

What are tables? Well, tables are like variables except they can have multiple values in one variable.
An example:

local hello = {"Computer", "Laptop", "Mobile", "Tablet", "XBOX"}

print(hello[2])

This is a table. Make sure you use those special brackets if not the table won’t work. When you do print(hello[2]), that will print out the value in the second position. If you number all the values, the position will be as followed:
Position 1 = Computer
Position 2 = Laptop
Position 3 = Mobile
Position 4 = Tablet
Position 5 =XBOX

So, when you print out the second one, it will print “Laptop” because laptop is in the second position.

Now, I will tell you guys about two codes you can write with a table.

  1. table.insert()

This inserts a value into the table. Like this:

This inserts the value “Controller” into the table at position number 2. Since, “Controller” and not “Laptop” is in second place, it will print out “Controller”

  1. table.remove()
    This removes a value from the table. Like this:

The table above removes the value at position number 4 which is “Tablet”. So, now if you print position number 4, it will print out “XBOX” because since one item was removed, the item after that shifted up a position.

Next one is the final topic!

In pairs loop

This is a slightly confusing topic so just try your best to understand. I will explain it to the best of my ability.
Here is an example of in pairs loop:

Let me explain what i and v means. i means index or the position the value is in. v means value. So, when you print i, it will print out what position each value is in, and when you print v, it will print out what the value is. So, if you saw in the output, it said:
1
Wheat
2
Flour
3
Sugar
4
Frosting
5
Icing

This is a basic introduction to i v or in pairs loop.

End
It has taken me hours(around 3-5) to write this post so I hope you guys can learn and benefit from this. If you have any questions, feel free to write a reply and I will try my best to answer them. If you have a suggestion to improve this post, please also leave a reply.
If you have any questions, you can watch the videos and look up articles on the developer hub, those are some great ways to learn.

Special thanks to @UseCode_Tap and @Alvin_Blox for teaching me all I have to know about the basics of scripting.

Hope you guys enjoyed this tutorial! See you in the next one!

139 Likes

Just for reference, this are also frequently called “nested if statements.” I’m sure both are used, just putting it out there in case someone hears “nested if statement” and isn’t sure what it’s referring to.

14 Likes

Thank you very much!
Here have some cookies :cookie::cookie::cookie::cookie: <3

10 Likes

This is absolutely lovely! I want to start coding, and I have a couple of playlists to watch, but I always prefer reading.

7 Likes

thank you so much for this! i am trying to learn scripting via youtube tutorials, however i prefer reading it :/D. i hope u make more in the future, if you have not already!

6 Likes