Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 7

Mapping Luau Concepts to Scratch

Recommended resources

Hello! In our previous lessons, we focused on building the world for our Obby game, placing platforms, gates, and other objects in Roblox Studio. Now, we're going to switch gears and learn how to bring our game to life by giving it instructions. This is done through scripting.

This lesson will introduce you to the fundamental ideas of scripting in Roblox's language, Luau. Since you have experience with Scratch, you'll find that many of the ideas are very similar, even if they look different. Our goal is to learn how to spot four key concepts in a Luau script:

  • Events: The triggers that make code run.
  • Variables: Containers for storing information.
  • Values: The actual information, like numbers or text.
  • Conditionals: Tools for making decisions in your code.

By the end of this lesson, you'll be able to look at a simple script and understand what its basic parts are doing, preparing you to write your own script in our next session.

From Blocks to Text

In Scratch, you build programs by snapping colorful blocks together. Each block is a command. Luau works the same way, but instead of using blocks, we type the commands as text.

Take a look at this image. On the left, you see some code made with blocks, similar to Scratch. On the right, you see the exact same instructions written in Luau.

This image compares block-based coding (left) to text-based Luau scripting (right). Notice how concepts like 'print' and 'if' exist in both, just with a different look.

As you can see, print "Hello, world!" in blocks becomes print("Hello, world!") in text. An if block becomes an if statement. The ideas are the same, but the way we write them is different. Throughout this lesson, we'll keep connecting these new text commands back to the Scratch blocks you already know.


Variables and Values: Remembering Information

In Scratch, you can create a "variable" to store things like a player's score or speed. You can think of a variable as a named box where you can keep a piece of information, or a value.

In Luau, we create variables to do the same thing: remember information that our game needs. The information we store is called a value, and it can be a number, a piece of text (called a string), or a simple true/false state (called a boolean).

To get a clear idea of how this works in Luau, let's watch a video that explains variables and the different types of values.

Roblox Luau Course - Episode 1: Fundamentals

This video from the channel Hoofer gives a great introduction to the fundamentals of Luau. We'll focus on the section about variables.

Please watch the segment on variables and data types. The video will explain how to create a variable and introduce the most common types of values you can store in them: strings (text), numbers, booleans (true/false), and nil (nothing).

As the video explained, the basic pattern for creating a variable in Luau is:
local variableName = value

  • local is a special keyword we use to create a new variable.
  • variableName is the name you choose for your "box" (e.g., playerScore, gateNumber).
  • = is the assignment operator; it tells the computer to put the value on the right into the variable on the left.
  • value is the information you're storing.

Here are the most common types of values, which you'll use all the time:

  • String: A piece of text, always surrounded by quotation marks.
    • local playerName = "SpeedyRunner"
  • Number: A number, with or without decimals.
    • local currentCheckpoint = 3
  • Boolean: Can only be true or false. These are perfect for tracking states, like whether a door is open or a power-up is active.
    • local hasFinishedRace = false

Conditional Decisions: If This, Then That

Games are all about rules and decisions. "IF the player has the key, THEN open the door." "IF the player's health is 0, THEN respawn them." In Scratch, you use if blocks for this.

Just like in Scratch, Luau scripts use conditional statements to make decisions and control the flow of the game.

Luau has if statements that work almost exactly the same way. An if statement checks if a certain condition is true. If it is, the code inside the statement runs. If it's not, the code is skipped. You can also provide an alternative action with else.

The next video segment will show you how to build these if statements in Luau. Pay close attention to how you check if two values are equal—it's a little different from a single = sign!

Roblox Luau Course - Episode 1: Fundamentals

We'll return to the same video to learn about if statements. This is how we'll eventually check if a player is at the correct gate.

Watch the part of the video covering if, else if, and else. Then, continue watching from the operators used in these statements. This section highlights important comparison symbols like == (is equal to), ~= (is not equal to), >, and <.

To summarize, the structure looks like this:

if condition then
    -- Code to run if the condition is true
elseif anotherCondition then
    -- Code to run if the first condition was false, but this one is true
else
    -- Code to run if none of the above conditions were true
end

The most important takeaway is that for checking if two things are equal, you must use a double equals sign ==. A single equals sign = is only for assigning a value to a variable.

  • local score = 10 (Assignment: "score gets the value 10")
  • if score == 10 then (Comparison: "is score equal to 10?")

To see how these ideas come together to solve a problem, watch this final clip from the video. You don't need to code along, just follow the logic.

Roblox Luau Course - Episode 1: Fundamentals

This part of the video presents a small challenge that uses variables and if statements to solve a problem. It's a great example of how these concepts work together.

Watch the "Billy's coins" problem and its solution. Notice how a variable holds Billy's balance and an if statement checks if he can afford the sword.


Events: Making Things Happen

So, we have variables to store information and if statements to make decisions. But when does the code actually run? In Scratch, you have "hat blocks" like When green flag clicked or When this sprite clicked. These are events.

In Roblox, events are also triggers that make our scripts run. A very common event in Roblox is Touched, which fires whenever a player's character (or any other part) physically touches another part. This is exactly what we need for our Obby gates!

The following video shows how to set up a script that "listens" for a Touched event.

The EASIEST Beginner Guide to Scripting (Roblox)

This video by SmartyRBX provides a clear, practical example of using an event to make a "kill brick" in an Obby. We won't make a kill brick today, but the way the Touched event is used is perfect for our lesson.

Watch the section explaining events and the Touched event. Pay attention to the structure Part.Touched:Connect(function() ... end). The code you put inside is what will run every time the part is touched.

The code killbrick.Touched:Connect(function(touchPart) ... end) might look complicated, but it's just the Luau way of saying "When the killbrick is touched, run the code inside this function." An event (Touched) is being connected to an action (the function).


Key Takeaways

We've covered the four big ideas that form the foundation of scripting in Roblox. Let's quickly review them with our Obby in mind:

  1. Events: These are the triggers. For our game, the main event will be Part.Touched, which will happen when a player runs through one of the gates.
  2. Variables: These are our memory keepers. We will use a variable to store each player's currentCheckpoint number.
  3. Values: This is the data we store. The checkpoint variable will hold a number (like 0, 1, 2). We'll also use strings to print messages and booleans to check conditions.
  4. Conditionals: These are our decision-makers. We will use an if statement to check: "is the gate number the player just touched equal to their current checkpoint number plus one?"

You are now prepared to move from theory to practice! In our next lesson, we will create our first Server Script in Roblox Studio. We will use the Part.Touched event and the print() command to send messages to the Output window, giving us a way to test our code and see it run in real-time.