Roblox Studio · Lesson 8

Build with code: automatically generate color ladders

Use Instance.new, Vector3 and loop to generate ladders, control spacing and height, and organize works into Folder.

10–15 years old 60 minutes Instance.new / Vector3 Hands-on practice Testing and Saving

Course overview

From a section of formula, a ladder that can be climbed grows.

The previous lesson allowed the code to control existing landmarks in sequence. This time we go one step further: let the program create Parts by itself and then calculate the position of each level.

You are the designer of this ladder. The order, spacing and height are all concentrated at the beginning of the script, and a new version can be made by changing a number. Start by doing small, safe steps before trying more challenging routes.

01 · BEFORE

Before starting

Understand for, table, Position, Size and Anchored. Create a new Lesson08 Baseplate.

02 · MAKE

Your work

Automatically generates a six-step ladder after running, all of which is placed in the Generated Stairs folder.

03 · CHECK

Complete flag

There are exactly six Steps in Explorer, and the ladder will not fall; the objects generated after Stop disappear.

BuildStairs generates six-level blue-green stairs, and Explorer displays Step01 to Step06
Figure 8-1 · Studio real shot: After running the complete script, a six-level ladder is generated. There are Step01–Step06 in GeneratedStairs on the right, and the Output below shows Created steps: 6. The green square in the front is the spawn point.

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 · Leave space and prepare a recipe

This lesson does not require manual creation of six Parts. Keep Baseplate and SpawnLocation, and create BuildStairs in ServerScriptService.

2

02·Create the object and set the properties

Instance.new("Part") will create a new Part, but it has not yet been placed in the scene. It only appears in the Workspace after setting the Parent to the workspace.

3

03 · Use index to calculate the position of each level

The height of the index level is 1 + index × RISE, and the forward distance is -index × DEPTH. The negative Z direction is the front of this practice map and does not represent the front of all camera frames.

4

04 · Understand two new tools

% is the remainder. Here (index - 1) % 2 + 1 alternately gets 1, 2, 1, 2, so the two colors are taken in turn. First calculate the results when index is 1, 2, and 3 on paper.

5

05 · It looks like stairs, but you have to be able to actually walk on them

The editor can fly and look at the model, but the player is limited by collisions, jumps and gravity. Only by walking from the birth point can you judge whether your spacing and height are appropriate.

Step by step

01 · Leave space and prepare a recipe

This lesson does not require manual creation of six Parts. Keep Baseplate and SpawnLocation, and create BuildStairs in ServerScriptService.

Start by leaving an open space of about 40 studs forward from the spawn point. The stairs created during runtime belong to the test copy; saving the script saves the recipe and does not automatically save the objects under test to the editing scene.

  1. Create a new normal Script: ServerScriptService > BuildStairs.
  2. Reserve Baseplate as a safe ground after a fall.
  3. First get familiar with the following Vector3 example, no need to run it: Vector3.new(8, 1, 5) means X width is 8, Y height is 1, and Z length is 5.
Official screenshot of inserting Script in ServerScriptService
Figure 8-2: Screenshot of official script insertion: Find the plus sign next to ServerScriptService, insert a normal Script, and name it BuildStairs.Roblox Creator Documentation · CC BY 4.0 · The original image has not been modified · Description of the permit

Step by step

02·Create the object and set the properties

Instance.new("Part") will create a new Part, but it has not yet been placed in the scene. It only appears in the Workspace after setting the Parent to the workspace.

Set the size, position and Anchored first, and finally set the Parent so that the object is ready when it enters the scene. Name is the name of the object, not the class that created it.

  1. First use a short experiment to generate a Step.
  2. Observe Explorer when Playing, and watch it disappear after Stop.
  3. Confirm that it remains motionless in the air, indicating that Anchored is in effect.
ServerScriptService > BuildStairs · Single Object ExperimentLuau
local step = Instance.new("Part")
step.Name = "Step01"
step.Size = Vector3.new(8, 1, 5)
step.Position = Vector3.new(0, 2, -8)
step.Anchored = true
step.Parent = workspace

Step by step

03 · Use index to calculate the position of each level

The height of the index level is 1 + index × RISE, and the forward distance is -index × DEPTH. The negative Z direction is the front of this practice map and does not represent the front of all camera frames.

The complete script first creates a Folder and then puts each level into it. This way you can fold the entire ladder and it won't get mixed up with hand-built parts.

  1. Replace all code in BuildStairs.
  2. After Play, expand GeneratedStairs and confirm Step01 to Step06.
  3. Go to the first level, try to jump to the highest.
ServerScriptService > BuildStairs · Full scriptLuau
local COUNT = 6
local RISE = 1.5
local DEPTH = 5
local colors = {
    Color3.fromRGB(60, 160, 235),
    Color3.fromRGB(90, 210, 175),
}

local stairs = Instance.new("Folder")
stairs.Name = "GeneratedStairs"
stairs.Parent = workspace

for index = 1, COUNT do
    local step = Instance.new("Part")
    step.Name = string.format("Step%02d", index)
    step.Size = Vector3.new(8, 1, DEPTH)
    step.Position = Vector3.new(0, 1 + index * RISE, -index * DEPTH)
    step.Color = colors[(index - 1) % #colors + 1]
    step.Anchored = true
    step.Parent = stairs
end
print("Created steps:", #stairs:GetChildren())

Step by step

04 · Understand two new tools

% is the remainder. Here (index - 1) % 2 + 1 alternately gets 1, 2, 1, 2, so the two colors are taken in turn. First calculate the results when index is 1, 2, and 3 on paper.

string.format("Step%02d", index) Write the number as two digits to generate a name like Step01. It just makes the Explorer tidier and doesn't change the behavior of the stairs.

  1. Add a third color to colors and observe that the colors cycle into three types.
  2. Change COUNT to 8, and the number of checked objects also becomes 8.
  3. Keep only one BuildStairs script at the same time to avoid overlapping generation.

Step by step

05 · It looks like stairs, but you have to be able to actually walk on them

The editor can fly and look at the model, but the player is limited by collisions, jumps and gravity. Only by walking from the birth point can you judge whether your spacing and height are appropriate.

If the height is too high, first adjust RISE back to 1.5. If the stairs are scattered, check whether each new Part is set to Anchored instead of just giving the Folder a "fixed" name.

  1. Test COUNT=1 and COUNT=8, both sizes should generate fine.
  2. Validation changes RISE only change climbing and DEPTH changes both the length of the platform and the distance to move forward.
  3. Save the script after Stop. If you want to keep the generated object as a model, you need to go through a clear editing process, which will not be done in this lesson.

Homework

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

1 Eight levels of rainbow ladder

Generate an 8-level ladder using three alternating colors.

2 Write the position formula

Calculate the Y and Z coordinates of the fourth level, and then check the Properties during operation.

3 Choosing: Width parameters

Add a new WIDTH constant, replace the hard-coded width 8 with WIDTH, and compare the two widths.

Completion criteria:First verify the work in Playtest, then click Stop and save it as "Initials_Lesson08" 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 may Instance.new not be visible after it is created?

The object has not been set to a parent of the visible scene, or the position is not within view.

Why is Anchored set at each level?

Fixed is an attribute of each Part, Folder will not be fixed for all sub-objects.

The ladder disappears after Stop. Is it lost?

No. It is generated by a running script and the test copy is restored after Stop; the script still retains the recipe.

Change COUNT to 8, do you need to copy the code?

No, the loop will create eight levels with the new number.

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