Roblox Studio · Lesson 28

Debugging laboratory: three bugs personally

Practice reading Output, adding breakpoints, observing variables, and using assert to turn "it seems to work" into a repeatable test.

10–15 years old 75 minutes Output/breakpoint/assert Hands-on practice Testing and Saving

Course overview

When a bug is discovered, instead of making ten random changes, the scope is narrowed down step by step.

Debugging is not about proving that it's wrong, but about looking for the difference between what computers actually do and what I want them to do. Every developer meets it.

You can record expectations, actual and minimal modifications so that the problem can be repeated by others.

01 · BEFORE

Before starting

Complete conditions, events and module lessons. Create a new Lesson28 and put only one experimental Script to avoid interference from old code.

02 · MAKE

Your work

Three interpretable repair records, and a piece of test code that can automatically check boundaries.

03 · CHECK

Complete flag

Reads the first false script name and line number; normal, boundary and failure are re-validated after restoration.

Official screenshot of setting breakpoints on the left side of Script Editor
Figure 28-1: Official breakpoint screenshot: Click next to the line number on the left side of the code to set a breakpoint and observe when the program reaches this line.Roblox Creator Documentation · CC BY 4.0 · The original image has not been modified · Description of the permit

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

01·Write a reproducible description of the problem

"The gold coin is broken" is too vague. "Enter the game, touch Coin once, and Coins change from 0 to 2" can help others find the problem.

2

02 · Object not found: first look at the path without waiting

Create Coin in Workspace, and then run the following small example of intentional errors in DebugLab. Cion is misspelled, FindFirstChild returns nil, and an error is reported when the next line accesses Color.

3

03 · No error is reported, or the calculation may be wrong

Writing >= as > will not generate a syntax error, but will reject players who "just meet the threshold". Test it with three numbers and you'll find it easier than trying it just once.

4

04 · One operation, two responses: check the connection location

If the number increases twice each time the button is clicked, first search whether there are two scripts, or the same event is Connected twice. The render function that updates the interface should not reconnect the event every time.

5

05 · Use breakpoints to pause at a critical step

Set a breakpoint next to the Script Editor line number and let the program stop before comparison or assignment during Play. Hover with a Watch or variable to see energy instead of guessing what it is.

6

06 · Keep Output and Scene clean

Meaningless print every frame will drown out the real problem; objects that are constantly Instance.new but not cleaned up will accumulate more and more. Measure and position first, then decide what to optimize.

Step by step

01·Write a reproducible description of the problem

"The gold coin is broken" is too vague. "Enter the game, touch Coin once, and Coins change from 0 to 2" can help others find the problem.

Only one reason is changed in each round of debugging. After making changes, return to the same starting point and repeat the operation to determine whether the repair is effective.

  1. Recording: Operational steps, expected results, actual results.
  2. Open Output and look first at only the earliest red errors associated with your script.
  3. Click the source or line number of the script and position it to the code.

Step by step

02 · Object not found: first look at the path without waiting

Create Coin in Workspace, and then run the following small example of intentional errors in DebugLab. Cion is misspelled, FindFirstChild returns nil, and an error is reported when the next line accesses Color.

The fix is to change the name to Coin and explicitly check if the object exists if necessary. Adding a wait to the wrong name will not create an object.

  1. Only run this deliberately incorrect code in an experimental copy of Lesson28.
  2. After seeing the error, record the line number, and then correct Cion to Coin.
  3. Replay Play, confirm Coin becomes green.
ServerScriptService > DebugLab · Intentionally wrong experiments, keep them after repairingLuau
local coin = workspace:FindFirstChild("Cion") -- 故意拼错
coin.Color = Color3.fromRGB(80, 220, 130)

Step by step

03 · No error is reported, or the calculation may be wrong

Writing >= as > will not generate a syntax error, but will reject players who "just meet the threshold". Test it with three numbers and you'll find it easier than trying it just once.

Below is the complete test after repair. assert will stop when the condition does not hold and indicate which case failed. There are no scene dependencies here and it is suitable for independent operation.

  1. Replace DebugLab, run the correct version, and see All boundary checks passed.
  2. Temporarily change >= in the function to >, run it again, and observe that the test equal to the threshold fails.
  3. Run it again after repair, don't just watch the red line disappear.
ServerScriptService > DebugLab · Fixed boundary testingLuau
local function canEnter(energy)
    return energy >= 5
end
assert(canEnter(4) == false, "Below threshold")
assert(canEnter(5) == true, "Exactly at threshold")
assert(canEnter(6) == true, "Above threshold")
print("All boundary checks passed")

Step by step

04 · One operation, two responses: check the connection location

If the number increases twice each time the button is clicked, first search whether there are two scripts, or the same event is Connected twice. The render function that updates the interface should not reconnect the event every time.

If you need to repeat and close functions over a long period of time, keep the connection object and, if not, Discontract. The normal buttons are usually connected only once at the start of the script.

  1. Looking back at Lesson 16, confirm that Activated:Connect is outside render.
  2. Add a print to the callback and observe how many times a click is executed.
  3. Do not mask duplicate connections with longer cooling; the source of duplication should be repaired.

Step by step

05 · Use breakpoints to pause at a critical step

Set a breakpoint next to the Script Editor line number and let the program stop before comparison or assignment during Play. Hover with a Watch or variable to see energy instead of guessing what it is.

Removes or disables breakpoints after debugging and runs again. Otherwise, the next test will suddenly be suspended and you will think the game is stuck.

  1. Set a breakpoint on the return line of the boundary test function.
  2. Runs and views the parameters in order of four, five, six.
  3. Continue execution until the test is completed, then clear the breakpoint.
  4. If the current version has different interfaces, look for tools such as Debugger, Breakpoints and Watch.
Official screenshot of the Watch window to view variables
Figure 28-2: Official Watch window: View the actual values ​​of key variables after pausing, turning guesses into checkable evidence.Roblox Creator Documentation · CC BY 4.0 · The original image has not been modified · Description of the permit

Step by step

06 · Keep Output and Scene clean

Meaningless print every frame will drown out the real problem; objects that are constantly Instance.new but not cleaned up will accumulate more and more. Measure and position first, then decide what to optimize.

The acceptance of this lesson is a repeatable result, not a claim of "no bugs at all". Keep a record of what you measured and what you didn’t measure so that you can continue next time.

  1. Remove temporary high-frequency output and retain useful stage or failure messages.
  2. Run a few more rounds and observe whether new objects with the same name are continuously added.
  3. Write down three fixed problems and corresponding verification steps.

Homework

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

1 A bug card

Write down the recurrence steps, expectations, actual conditions, reasons, modifications and re-test results.

2 Boundary test extension

Change the threshold to 8, and add a test for each of 7, 8, and 9.

3 Selection: review of old works

Check previous projects for duplicate scripts, loops without waits, or uncleaned connections.

Completion criteria:First verify the work in Playtest, then click Stop and save it as "Initials_Lesson28" 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.

Does the absence of a red error mean the logic is correct?

It does not mean that, for example, if the boundary comparison is written incorrectly, it may run normally but give wrong results.

Why read the first related error first?

The subsequent errors may just be a chain reaction caused by the previous problem.

Can the repeats be fixed only by cooling?

Cooling may mask the phenomenon, and duplicate scripts or duplicate Connects should still be looked for.

What do you want to check after the debugging?

Clear the breakpoint, run normally again, and repeat the original reproduction steps.

Explore further

This is where we start when we need to check the data.

The text code can be copied directly. First confirm the script type, placement, and whether this code is a complete version or a small experiment; do not run multiple versions of the same script superimposed. The source of the interface reference picture has been marked. The object names in the picture may be different from those in this lesson. Please refer to the operation steps.

Download the code and placement instructions for this lesson