What is Programming?
To put it simple, Programming is the way how we talk to computer, and ask computer to complete tasks using computer languages.
We can’t just ask the computer to calculate 1 + 1 with human language:
We will have to use a language that computer understands: A Programming language:
How do I program in Roblox?
Same as our human society, there are many languages in the programming world.
Here are some examples of programming languages:
- C
- C#
- C++
- Python
- Java
- HTML/CSS/JavaScript
In Roblox Studio, we use a language called “Luau”:
We will be using Luau to explain some of the basic blocks of programming. Here are the topics we will cover:
- How to create a script file in Roblox
- Script editing
- Input and Output
- Variables
- Conditional Statement
- Loop
- Array
- Functions
1. How to create a script file in Roblox
Please refer to this tutorial HERE if you are not sure how to create script in Roblox Studio.
2. Script editing
Please refer to this tutorial HERE if you are not sure how to edit or type a script well.
3. Input & Output
It is just like using a blender when we program:
We need to put in the apples (input) to make apple juice (output):
Same as programming in Roblox Studio, to ask the computer “1 + 1 = ?” we will put our math into our script:
The word “print” means to show something within the ( ) using the “Output” window.
(Here is how you show your Output window if you don’t see one)
Now, press “Run”:
In our “Output” window, you can see the result “2”:
Challenge 1:
Try changing the numbers inside print(1+1) to generate the following results:
- 10
- 15
- 20
- 30
In the programming world, our math operants look a bit different than our usual ones:
- Plus: +
- Minus: –
- Multiply: *
- Divide by: /
Try using these in your challenge as well!
4.1 Variable
Imagine you now have three empty boxes in front of you:
Now you want to use these three boxes to organize your room. So you put some books, some clothes and some potato chips in each of the boxes:
And of course, you are going to name each of the box so that you know which one is which:
Same in the programming world, we can create these boxes to store data for future use.
Let’s take a look how to do it in Roblox Studio (Luau):
Let’s take a closer look at line 1:
- To tell the computer that the variable is within the local scope. For now don’t worry too much about it.
- The name of the variable. It could be anything: books, trees, HP, atkDamage, etc.
- The data we want to store into the variable. You can store 4, 40, 400000, or 15948657, any number that isn’t too big or too small.
- This equal mark is different than what we learn in our math class:
- In math: . LEFT ===>>> RIGHT.
- In programming: LEFT <<<=== RIGHT. We use it to store data into variable(s).
Now, let’s print the variable books on the Output Window:
Challenge 2: Try printing out the other two variables on the Output window!
Challenge 3: Create a variable name age, put your age into this variable, and print it on the Output window!
Challenge 4: Answer the following question (Don’t use Roblox Studio!):
4.2 Basic Data Types in Luau (number, string, boolean)
You are now creating a RPG game…
Take a look at this player’s information:
This player is level 1, with 100 HP and 15 attack point.
The player’s name should be デジラボ子, however, you see a small red line under it:
The red line means there is an error at the position.
Besides storing numbers into variables, we can store words or messages into them as well!
In programming, we call the words or messages a string. To create a string, we need to put “” around the words (Shift + 2):
Now, let’s print all the information of the player using print() and comma “,”:
Challenge 5:
- Create your own string variable between line 4 and line 6
- Name it whatever you want
- The variable should store a winning message when beating a monster
- Print the message out using print().
After finishing Challenge 1, your script should look like this:
Now, another type of variable is called boolean.
A boolean variable only has one of the two possible value:
true OR false.
Let’s now create two more variables above print, and name them hasSword and hasShield:
Very simple: true means YES, false means NO.
In this player’s case, he is now holding a sword, but not holding a shield.
Challenge 6:
- Create your own player status using a boolean variable!
- (If you can’t think of any, set up a new variable, name it hasShoes , and set it to false.)
4.3 Change the value of variables (=)
Let’s make sure your code looks like this:
We will use print() to imitate the story of our RPG game.
Type the following (starting from line 10):
Game start!
You found a slime!
(from line 13):
Battling…
And you won the battle! (from line 17):
(the winning message you set earlier)
Level UP!
ATK UP!
HP DOWN.
Now, we are going to change the value of the variables we have set earlier(level, hp, atk), one by one from line 22:
Challenge 7:
Using the hint above at line 22,
- Set the variable hp to 90
- Set the variable atk to 17
…Answer below…
…
…
…
Here is the answer of Challenge 1:
Let’s print them all out like this, with description:
(You may copy line 26 to line 28 to the line before the game starts:)
Hit “Run” to check your program if it is working: