Roblox Wiki
Advertisement
Roblox Wiki
Tutorial page
This article is an easy tutorial.

Introduction

Welcome! If you're new to scripting, you've come to the right place. This is a complete beginner tutorial series that will teach you the fundamentals of Roblox scripting. I know you want to start clicking on things and learning things, I want to take a minute to explain what you will and won't learn.

Author's Notes

  • Note that this tutorial, as well as the entire series, will not magically turn you from a newbie to an expert. You can have all the tutorials and tools in the world, but ultimately, the one thing that will improve your skills as a developer is practice and patience. So take the time to make sure you truly understand each concept before moving on.
  • If you are truly stuck, remember the Pareto principle, or the 80/20 rule: 20% of the features and concepts are used 80% of the time. In other words, there is almost always more than one way to achieve a certain result.
  • If there are concepts that remain unclear in this tutorial, feel free to ask an expert or look up a tutorial on YouTube.
  • Before we begin, reading the Newbie's Guide to Scripting is recommended.

Inserting a Part

Open Roblox Studio for the first time and create a Baseplate in the "New" tab, and you'll see this big area. If you don't see anything but that baseplate, don't worry, you don't have to put anything in there for now.

50% of your time is spent in the viewport. If you're modeling or moving something around, you'll do it here. Let's talk about that more.

If you go down into your Explorer panel on the side, you'll see a "Workspace" tab. This tab holds all Parts in the game. If you do not see an Explorer Window, go to the View Tab, then turn on the Explorer window. You'll also need the Properties window.

Go back to the Home window and click Part (the button with the cube on it.) A gray brick should appear on the baseplate. To move it around, you can use the "Select" tool or the "Move" tool. You can move around the part along one dimension by pulling on one of the arrows using the Move tool.

You can also resize the part using the "Scale" Tool and rotate it using the "Rotate" tool.

Now you need to know how to insert other parts into your game, like fire. Go to the Explorer window and then find the Workspace tab. Open the arrow beside it and find the Part. Then, hover over the Part tab, click the plus sign, and insert a "Fire" object. If you cannot find it, you can use the search bar.

Congratulations, you just created your first fire part! But what about scripting? We'll get to that right now...

Properties

In the Newbie's Scripting Guide, you learned a had a very broad understanding of a property, which we'll explain in more detail now. A property is a value attributed to a part that makes it look like what it is. For example, the Transparency, Size, and Position are all properties of a part. Property values are the numbers or words that are used to set a property. How do you do that? You can go to the Properties window, find the property, then type in the value.

The main properties a beginner should know are:

  • Transparency: the amount of light that passes through the object. This is a number value, where 0 means completely opaque and 1 is completely invisible.
  • BrickColor: the color of the brick.
  • Color3: an alternative to BrickColor, only it takes 3 number values.
  • Name: the name that is attributed to the brick.
  • CanCollide: whether or not other objects will pass through the object; if the checkbox is flicked off, objects could pass through it uninterrupted.
  • Anchored: whether or not the object is subject to change position. If the checkbox is turned on, the object will not be affected by gravity.

But how do you set a property using a script? Well...

Making Paths

You should have already have a basic idea on how to make a path if you have read the Newbie's guide, but if you haven't, this section will explain it briefly.

Create a part and a script into the Workspace. Delete any existing code in the script.

  • Tree-collapse.png
    ExplorerImageIndex 19.png
    Workspace
    • Tree-expand.png
      ExplorerImageIndex 1.png
      Part
    • ExplorerImageIndex 6.png
      Script

First we need to access the part. Start by typing "game".

1 game

The word "game" should turn blue (or red if you had dark mode enabled.) Now, access the Workspace, by typing a period, then "Workspace".

1 game.Workspace

Then, access the Part. You'll notice that the part tab has a little indent compared to the Workspace tab. We say that the Part is the Workspace's "child," and that the Workspace is the Part's "parent." In Roblox, the Explorer window is organised sort of like a hierarchy, where objects are placed on a sort of "rank."

1 game.Workspace.Part

Of course, if you had renamed the Part, you'd need to replace "Part" with the name you gave it. Also, make sure that no two parts are given the same name: otherwise the script won't know which part to access.

Now we set the property. We say that the property is a child of the part. So...

1 game.Workspace.Part.Transparency

To set the value, type an equal sign, followed by the value. So the completed code would be...

1 game.Workspace.Part.Transparency = 1

Spaces around the equal sign is optional, but it makes the script neater. So, when you playtest the game, you'll see that the part suddenly vanishes. In reality, it had just been made invisible by the script.

Alternatively, in the Explorer window, drag the script onto the part, if it is done correctly, it should look like this:

  • Tree-collapse.png
    ExplorerImageIndex 19.png
    Workspace
    • Tree-collapse.png
      ExplorerImageIndex 1.png
      Part
      • ExplorerImageIndex 6.png
        Script
1 script.Parent.Transparency = 1

There are a few new terms here. When you say "script," the game will assume it means the script the text is on. When you say Parent, instead of looking for the script's child, it'll access its parent.

If you had made a mistake, chances are the game will have noticed it. Go to the View Tab, then click on "Output." A window will appear, and if the game found an error, it would print an error message nice and bold in red. Clicking on the red message will take you to the source of the problem.

Note: If you need to set a text value, place the value in quotations. Also, Color3 and BrickColor values are done differently, and I'll need to cover that later.

An alternative way to access a child is by wrapping the name in quotations and square brackets.

1 game.Workspace["Killbrick"]
2 game.Workspace.Killbrick

Both of the above lines serve the same function. However, you must use the square brackets method if the name of the object contains spaces. (For example, if "Killbrick" was renamed to "Kill brick," you would use the square brackets method.

Now, things will get a tad bit more complex. A "folder" is an object that can be inserted into the Workspace that holds multiple objects together as a sort of organizational tool. A "model" is another object that serves a similar function. Oftentimes developers will place models inside folders. A good rule is to start with "game," then work your way down to the object.

  • Tree-collapse.png
    ExplorerImageIndex 19.png
    Workspace
    • Tree-collapse.png
      ExplorerImageIndex 77.png
      Folder
      • Tree-collapse.png
        ExplorerImageIndex 2.png
        Model
        • ExplorerImageIndex 1.png
          Part
1 game.Workspace.Folder.Model.Part.Transparency = 0

You can see that this shows a more complicated hierarchy. The part is placed in a model, inside a folder, in the Workspace. Of course in these complicated situations, names such as "Folder," "Model," and "Part" should be renamed to prevent confusion. A good rule of thumb is that if something should be accessed in a script, it should ALWAYS be renamed.

Print

Printing is an essential part of debugging your game. By default, the following code should already be in your script when you create one:

1 print("Hello world!")

Now, go to the View tab on the top of the screen and click "Output." A window should appear.

If you play your game, you will see that "Hello world!" was posted in the Output window. It has no function in a live server, but is essential should you come across a bug you can't solve.

Variables

Now we discuss variables. Variables are, in short, placeholders for data. It would store information such as numbers and text. Then, in future use, instead of typing in those values, one can just mention the variable. Think of them like a colloquialism in the English language (for example "kinda," "dunno," or "ok"). These words are simply a way to informally shorten a sentence.

When we first mention a variable we are declaring it. This is how we do it:

1 local Var = 0

Here, we are declaring a variable named "Var". We are also establishing that the variable var is equal to 0.

The local is optional, but it is common practice to use it always because it is faster for the system to obtain.

Note: This is case sensitive. Therefore, "var" and "Var" are two completely different variables.

There are 5 main types of variable values:

Instances

All these are instances.

An instance is an object in the Explorer. This refers to parts, light objects, and more. An example of this is the Parent property of a part.

To set an instance variable, make a path, as explained above:

1 local Car = game.Workspace.Car

Integer Values

An int value, otherwise known as an integer, is basically any number that is not a decimal. An example of this is the Brightness setting of a light object.

To set an int value, you can simply enter the number. Note that the number should turn aqua (or yellow for dark mode).

1 local IntVar = 12

Float

A double-precision floating point format, often shortened to float, is any number that allows for decimals. Usually, they never have more than 3 decimal places. Just like int values, you can simply enter the value.

1 local FloatVar = 0.02

Boolean

A boolean, otherwise known as a bool value, is a value that is either true or false (yes or no). In the Properties window, the properties with a checkbox is a boolean.

Checkboxes imply booleans.

To set a boolean, enter either true or false.

1 local boolvar = false

Strings

A string is any text that uses letters, symbols or spaces. To enter one, you must wrap it in quotation marks.

1 local StringVar = "Cheese"

Here, the word "Cheese" is wrapped in quotations.

Of course, there are other data types, like CFrame and Enum, but we'll get to those later.

Nil

Nil, also known as null in other languages, is nothing; the absence of any data. Nil is not a data type in its own right, therefore variables of any data type can be set to nil. When referring to Instances, nil refers to an object that does not exist.

Accessing Variables

Now, we need to know how to use the variables. If you want to do something with a variable, just substitute the value with the variable name. What I mean is that both of these scripts yield the same results.

1 --Script 1
2 game.Workspace.Car.CanCollide = true
3 
4 --Script 2
5 local Car = game.Workspace.Car
6 Car.CanCollide = true

You can see that variables are direct substitutions. Note that if you are accessing or updating a variable, do not use local. Only use local the first time the variable is mentioned.

However, this lengthens the script, and makes it more complicated. In this case, the first script would be more convenient than the second. However, variables are mostly used when a value is mentioned more than once.

But what's the difference between these two lines?

1 print(VariableName)
2 print("VariableName")

When you put data in strings, you need to place them in quotation marks. So, in the second line, you are telling the script to print the string, so you would see "VariableName" in the output window. In the first line, you are telling the script to print the variable, so it would print the value. However, make sure the variable name is spelled correctly, otherwise it'll get an error.

Now what is wrong with this script?

1 Car.Transparency = 1
2 local Car = game.Workspace.Car

Remember, Roblox Lua uses the same writing system as English; it is read left to right, top to bottom. Therefore, the script won't know what "car" is until it reaches line 2. Line 1 will break.

Note that variables cannot be accessed from other scripts.

The .new() Constructor

Earlier, I left you hanging on how to set colour values.

BrickColor allows you to use a color from a preset list of colors that can be found here.

1 script.Parent.BrickColor = BrickColor.new("Really red")

The above script changes the BrickColor to "Really red," one of the colors in the list.

We call this a .new constructor. Basically, for certain data types, including BrickColor, we need to use a .new constructor to create a value for the data. Generally, it would work like this:

1 DataType.new(value here)

Color, however, is done differently, as it uses Color3. This is a data type that uses numbers, not names. There are 3 constructors for this data type, and see this tutorial if you wish to see an explanation for this constructor.

1 script.Parent.Color = Color3.new(0,1,0)
1 script.Parent.Color = Color3.fromRGB(230,230,230)
1 script.Parent.Color = Color3.fromHSV(0,1,0)

Vector3

There's another exception to setting properties. You will notice that the Roblox place is three-dimensional. It has three values: X, Y, and Z, each for one of the dimensions. Position and Size both use three values: X, Y, and Z. To set these, we use Vector3.

1 script.Parent.Position = Vector3.new(1,2,3)

Here, I am setting the X coordinate to 1, the Y coordinate to 2, and the Z coordinate to 3. Again, we use the .new constructor.

To learn to set Orientation, read here.

Comments

Now, what if we don't want to forget what each line does? It'd be such a hassle to have to read each line over and over again. So, placing 2 dashes, followed by the text, will make the script ignore the text.

1 script.Parent.Transparency = 1 --Makes brick invisible.

In this case, the script will perform the code, before ignoring the text you wrote after.

Functions

Which brings us to the final topic of the beginner tutorial: functions! A function, like a variable, is another way to simplify code. Think of it like leaving your wallet in your pocket instead of your bag to make it easier to get. A function is a piece of code that can be used over and over again. Like variables, we declare them. We do it like this:

1 local function Name()
2     --Code
3 end

A few notes here:

  • Local function creates the function.
  • The blue text is the name of the function.
  • The ( ) is reserved for parameters, which I will explain in the intermediate tutorial. However, regardless, all functions must be followed by ( ).
  • It is conventional that the code inside the function is indented. This should be placed in your script automatically. However, if for some reason it doesn't, go to the top of the screen > Format Document.
  • A function also has to be closed with the word end. Again, this is placed automatically.

That is a lot of points, but it will become second nature when you practice using them more often.

To make it actually work, though, we call the function. We do it just like this:

1 Name()

And that's all you need to know for now! Go over to the intermediate tutorial to continue. Good day.

Advertisement