Roblox Studio · Lesson 3

Introduction to Luau programming: from the first line of print() to using code to control Part

Create the first ordinary Script in ServerScriptService, understand print(), Output, local variables and object references, and use short codes to safely modify Parts.

60 minutes After Lesson 1-2 Luau Script Editor Output local variables

Course overview

From mouse building to "let the game act according to the rules".

In the first two lessons, students used Move, Scale and Rotate to create static scenes. Starting from this lesson, we write instructions for the computer to execute in sequence. The scripting language used by Roblox is called Luau; we often say Lua every day, but in the Roblox project, Luau is the more accurate name.

This lesson emphasizes a reliable cycle: write a small piece of code, click Play, read Output, click Stop, change only one question, and test again. Short codes, early testing, and reading the first error are habits that will be repeated in all subsequent scripting classes.

01 · SCRIPT

Give the command a position.

Put ordinary Script into ServerScriptService, and the name indicates the purpose.

02 · OUTPUT

See the program running

print(), warn(), script name and line number together help us locate the problem.

03 · VARIABLE

Name the data

Use local to save strings, numbers, Boolean values, and object references.

Lesson03_FirstScript and the first line of print code in ServerScriptService
Figure 3-1: The first normal Script is located in ServerScriptService, and the script editor has written print().

Learning route

Prepare first, then do it; after seeing the result, remember to save it.

Every time a small step is completed, it is tested. Say what you expect to see first, and then use Play and Output to check it. Don't rush to write all the code at once.

1

Save Lesson03

Save from Lesson02 and prepare an Anchored Lesson03Part.

2

Create a normal Script

Create Lesson03_FirstScript in ServerScriptService.

3

Runprint()

Seeing Hello, Roblox through Playtest in Output!

4

Create local variables

Save string, number and boolean respectively.

5

Reference object

Use WaitForChild to find Lesson03Part, and then modify the properties.

6

Read First Error

Fix issues based on script name, line number, and error description.

First script

Use comments and print() to make the first code visible.

Ordinary Script is placed in ServerScriptService, with a clear location, and it is also convenient to identify server messages in Output. Rename it to Lesson03_FirstScript immediately after creation. Do not keep the default names such as Script and Script1 for a long time.

Double dash -- followed by a comment, which only helps people read, but will not be executed. print() will send the content in parentheses to Output. Even if the screen does not change, it can prove that the script has been run.

Lesson03_FirstScriptLuau
-- 第3课:第一个 Luau 脚本
print("Hello, Roblox!")
  • Expand ServerScriptService in Explorer, click the plus sign and select Script.
  • Press F2 to rename and double-click to open the Script Editor.
  • Click Play to read text, Server, script names and line numbers in Output.
  • Click Stop before editing the script.
Output shows Hello Roblox and script source
Figure 3-2: After Playtest, Output displays the print() message and its source.
Entering rules:Use English half-width quotation marks, brackets and commas; the code is case-sensitive, score, Score, SCORE are different names.

Local variables

local is to prepare a name with a clear purpose for the data.

A variable is like a box with a name: the right side of the equal sign is the value to be saved, and the left side is the variable name. In the beginner stage, use local to create local variables, so the scope of scope is clearer. Variable names use camelCase, such as courseName, playerScore, newWidth.

string stores text, number stores numerical values, and boolean only has true or false. Numbers use + operation; strings use .. to connect. Don't add words and numbers directly using +.

Variables and OutputLuau
local courseName = "Roblox Studio"
local score = 10
local isReady = true

print(courseName)
print(score)
print(isReady)
  • Each line does only one main thing.
  • The variable name should express its purpose and should not be replaced by a, b, or c.
  • Checks if the red wave lines, quotation numbers and brackets are paired before running.
Create string numbers and Boolean variables in Luau
Figure 3-3: local saves string, number and boolean.
Output displays the string number and Boolean value of the variable
Figure 3-4: Output displays three variable values ​​in sequence.
Small exercise:Create coins=7 and bonus=3, calculate total; then use .. to connect “Space” and “Obby” into the game title.

Object reference

Save the Part reference first, and then use the dot to safely modify its properties.

Create and accurately name Lesson03Part in the Workspace, then check Anchored. workspace:WaitForChild("Lesson03Part") will wait for the object to appear and return its reference; after getting the reference, you can access Name, Color, Size and other properties through dot notation.

Color and size changes during Playtest belong to the temporary running world. Clicking Stop will usually revert, and this is the value of security testing: verify the behavior before deciding whether to permanently modify it in the edit state.

Modify Lesson03PartLuau
local lessonPart = workspace:WaitForChild("Lesson03Part")
local newWidth = 14

lessonPart.Color = Color3.fromRGB(255, 170, 0)
lessonPart.Size = Vector3.new(newWidth, 2, 6)
print("已修改:", lessonPart.Name)
  • The object names must be exactly the same; Lesson03Part is different from lesson03part.
  • Color requires Color3; Size requires Vector3 containing X, Y, Z.
  • Change newWidth from 14 to 8 and play the comparison result again.
Script get Lesson03Part and change color and size
Figure 3-5: Use variables to save object references, and then modify Color and Size.
Orange and widened Lesson03Part in Playtest
Figure 3-6: Part turns orange and widens during runtime; it will return to the editing state after Stop.
Minimum verifiable changes:First change only one attribute and print the object name. After you see the results, expand the code; don't add a lot of unknown code at once.

Debug loop

The wrong message is not a failure, it's Studio giving you the location clue.

When a red message comes in, the fixed processing order: first look at the earliest error, then look at the script name and line number, click on Stop, go back to the corresponding position to check for symbols or names, and restore Play. The error behind this is probably only a chain result caused by the first syntax problem.

For example, when the string lacks closing quotes, do not delete the entire code; add pairs of English quotes, retest, and confirm that Malformed string no longer appears in Output.

Errors and fixesLuau
-- 错误版本
local message = "Lesson 03
print(message)

-- 修复:给 Lesson 03 补上右引号
local fixedMessage = "Lesson 03"
print(fixedMessage)
  • Find the top red message first.
  • Confirm the script name and line.
  • Only change one question after Stop.
  • Replay Play, confirm normal output and error disappear.
String syntax error displayed in Script Editor and Output
Figure 3-7: When the closing quotation mark is missing, both the editor and Output will give positioning clues.
Safety habits:Only run shortcodes from your own class projects; do not copy and run scripts from Toolbox, chat messages, or unknown models.

Homework

It's your turn to change and make your own version.

1 Three variables

Use gameTheme, levelCount, and isComplete to save text, numbers, and Boolean values ​​respectively.

2 Three clear outputs

Each print has label text to let readers know what the value they see represents.

3 Once attribute modification

Safely modify the Color, Size or Transparency of Lesson03Part and fix a minor bug.

Completion criteria:First verify the work in Playtest, then click Stop and save it as "Initials_Lesson03" or the corresponding Homework copy; do not overwrite the runnable version of the previous lesson.

Exit questions

Answer it yourself first, and then expand on it.

Why are ordinary Scripts placed in ServerScriptService?

Clear location makes it easy to manage server-side scripts and identify their messages in the Output.

What is the function of print()?

Send information to Output to confirm whether the code is executed and observe the data.

What do local and = mean respectively?

local creates a local variable; = assigns the value on the right to the variable name on the left.

Why fix the first red message first when an error occurs?

Subsequent errors may be caused by a chain of syntax errors in the first place.