Roblox Studio · Lesson 7

Table & List: Ordering Treasure Search Routes

Saves signs, rewards and colours with tablet, reading indexes, dictionaries and browsing to highlight your own treasure hunt.

10–15 years old 60 minutes table / ipairs Hands-on practice Testing and Saving

Course overview

Put three road signs into a table and light them in order.

When there is only one landmark, one variable is enough. The road signs become three, ten, and you don't want to duplicate the same set of code for each road sign. A table can help you put related data together.

This course is organized in two ways: sequenced arrays and name-based dictionary. They are all tablets in Luau.

01 · BEFORE

Before starting

Complete Lesson 6, Understanding for Loops and Waits. Use the standalone Lesson07 Baseplate.

02 · MAKE

Your work

A signposted route lit in the order Camp → Bridge → Cave.

03 · CHECK

Complete flag

By adjusting the order in the table, you can explain the difference between roote[1] and settings.reward.

Screenshots of the characters and dialogue scenes of the official array exercise
Figure 7-1: Official array application reference: multi-sentence dialogues can be stored in the array in order. This lesson applies the same idea to the three road signs: Camp, Bridge, and Cave.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·Give accurate names to the three road signs

First use the operations of Lesson 2 to lay out the route, and the code is responsible for determining the order of lighting. The name will be a clue to what the program is looking for, and must be case-sensitive.

2

02·An array is like a row of boxes lined up

Luau arrays usually start at 1: route[1] is the first item, route[3] is the third item. #route can read the length of this list without holes.

3

03 · Dictionary allows settings to explain their uses

settings.reward is easier to read than settings[2] because reward already indicates that this number is a reward. Dictionaries are suitable for data with names such as colors, duration, rewards, etc.

4

04·The same action is given to three road signs in sequence

The complete script below separates "who to deal with" and "what to do". route is responsible for object order, and the loop body is responsible for changing colors.

5

05 · Change data, do not change the structure randomly

Adding a fourth station requires adding scene objects and list items at the same time. If you only add a string, if there is no corresponding Part, the program cannot find it; if you only add a Part, if it is not added to the list, it will not light up.

Step by step

01·Give accurate names to the three road signs

First use the operations of Lesson 2 to lay out the route, and the code is responsible for determining the order of lighting. The name will be a clue to what the program is looking for, and must be case-sensitive.

Each landmark remains independent. Don't put them into the Model first, otherwise workspace:WaitForChild(name) in this lesson will not find the direct child objects.

  1. Create three Anchored Parts and name them Camp, Bridge, and Cave respectively.
  2. Size is set to 6, 1, 6, Position is 0, 1, -8; 0, 1, -18; 0, 1, -28 respectively.
  3. Create RouteLights in ServerScriptService.

Step by step

02·An array is like a row of boxes lined up

Luau arrays usually start at 1: route[1] is the first item, route[3] is the third item. #route can read the length of this list without holes.

ipairs will give the index and value in sequence according to 1, 2, 3... and stop when encountering the first missing value. Don't dig random holes in the array with route[2] = nil.

  1. First run a short experiment and observe index and name.
  2. Exchange the first item with the third item and re-predict the output order.
ServerScriptService > RouteLights · Array experimentLuau
local route = {"Camp", "Bridge", "Cave"}
print("First stop:", route[1])
print("Stop count:", #route)
for index, name in ipairs(route) do
    print(index, name)
end
Output window and filter control reference
Figure 7-2: Official Output interface reference: Check the index and name printed by ipairs, and confirm that the route sequence is 1, 2, 3.Roblox Creator Documentation · CC BY 4.0 · The original image has not been modified · Description of the permit

Step by step

03 · Dictionary allows settings to explain their uses

settings.reward is easier to read than settings[2] because reward already indicates that this number is a reward. Dictionaries are suitable for data with names such as colors, duration, rewards, etc.

The dictionary does not have the route sequence of "first stop, second stop". Don't rely on the order in which pairs are traversed through the dictionary to arrange levels.

  1. Add the following settings at the end of the experiment just now and read reward.
  2. Change reward to 20 and confirm the output change; this step is just a data experiment and no gold coins have been issued yet.
End of RouteLights · Dictionary experimentLuau
local settings = {
    reward = 10,
    pauseSeconds = 1,
    activeColor = Color3.fromRGB(90, 220, 150),
}
print("Reward preview:", settings.reward)

Step by step

04·The same action is given to three road signs in sequence

The complete script below separates "who to deal with" and "what to do". route is responsible for object order, and the loop body is responsible for changing colors.

WaitForChild(name) uses a string to find an object. When the name is misspelled, it will wait and may prompt Infinite yield in Output; that is not because the program is slow, but that the object path needs to be checked.

  1. Replace RouteLights with full code.
  2. After Play, wait for the road signs to light up in sequence, and each road sign only lights up for one second.
  3. After Stop, only the route order is changed, the loop body is left unchanged, and the verification route is changed accordingly.
ServerScriptService > RouteLights · Complete scriptLuau
local route = {"Camp", "Bridge", "Cave"}
local settings = {
    pauseSeconds = 1,
    activeColor = Color3.fromRGB(90, 220, 150),
}

for index, name in ipairs(route) do
    local marker = workspace:WaitForChild(name)
    local originalColor = marker.Color
    print("Stop", index, name)
    marker.Color = settings.activeColor
    task.wait(settings.pauseSeconds)
    marker.Color = originalColor
end
print("Route complete")

Step by step

05 · Change data, do not change the structure randomly

Adding a fourth station requires adding scene objects and list items at the same time. If you only add a string, if there is no corresponding Part, the program cannot find it; if you only add a Part, if it is not added to the list, it will not light up.

When the list is arranged continuously, table.insert(route, "Tower") can add an item; table.remove(route, 2) will delete the second item and move the following items forward.

  1. Add Anchored's Tower, and then add "Tower" to the end of route.
  2. Confirm that Output has four stations 1, 2, 3, and 4.
  3. Try to read route [99], which turns out to be nel; don't change nar col.

Homework

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

1 Four Stations Treasure Hunt

Create a fourth waypoint and change the route to Camp → Cave → Bridge → Tower.

2 Slower and clearer

Only adjust settings.pauseSeconds so that each station lights up for two seconds.

3 Selection: second round of route

Use a function to wrap the lighting process, pass in a different route table, and run it again.

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

What is route[1] in this lesson?

String Camp; it is not Part itself, you need to use this name to find the corresponding object.

Why use ipairs instead of relying on pairs order?

Routes are subject to successive numerical indexes.

Is settings.reward a gold coin system?

Not yet. It's just a field where you save numbers. The actual coin added to the player will be realized in class 10.

Is it enough to just change the array when adding new road signs?

Not enough, you also need to create a Part with the same name in the correct path.

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