Roblox Studio · Lesson 19

Attributes and tasks: Collect three energy crystals

Use Attributes to record the collection progress of each player so that task objectives and reward rules can be clearly checked.

10–15 years old 75 minutes Attributes / task status Hands-on practice Testing and Saving

Course overview

Three crystals, one task, everyone has his own progress.

The gold is suitable for repeated collection, and the items of the mission are usually calculated only once. This time you have a clear goal: to find three different energy crystals.

Attributes are like small labels attached to objects. We log Crystals and QuestComplete on the Player, making progress easy to read and inspect in Properties.

01 · BEFORE

Before starting

Understand table, Player and server scripts. Create a new Lesson19, which does not rely on the old gold coin system.

02 · MAKE

Your work

Three crystals that each person can collect once. After collecting them all, set QuestComplete to true.

03 · CHECK

Complete flag

No progress will be made by touching the same crystal repeatedly; only three different crystals can be completed; two people will not steal the task items from each other.

Official screenshot of the Attributes area in Properties, the attribute name is official example
Figure 19-1: Official Attributes attribute area reference: This lesson creates Crystals and QuestComplete, please refer to the names in the code.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 · Use Folder to put task items together

Create a folder containing only crystal parts, and the script can find them all at once. Don't put lights, signposts, and scripts in there that the program might mistake.

2

02·First recognize the two progress labels

Crystals saves the collected quantity, QuestComplete saves whether it is completed. SetAttribute sets a value, and GetAttribute reads it; they are different from the built-in Position and Color, and are self-defined labels.

3

03 · Use "Player → Already Received Crystal" to prevent duplication

collected[player] is the collection table of this player, which uses crystal objects as keys. Touching the same object repeatedly will return immediately, while touching another one will continue processing.

4

04 · Visible objects may not still be available for collection

The same crystal may have been claimed by A, but is still available for B, so the shared appearance cannot simply represent everyone's task status.

5

05 · Check the meaning of "three different ones"

The task is completed not by touching three times, but by touching three different crystal stones. Write this sentence down first, and then check whether the code complies with it.

Step by step

01 · Use Folder to put task items together

Create a folder containing only crystal parts, and the script can find them all at once. Don't put lights, signposts, and scripts in there that the program might mistake.

For simplicity, this lesson keeps the spar visible. Whether each player has collected it is determined by the server's records, not by global hidden crystals.

  1. Create Folder: Crystals in Workspace.
  2. Create CrystalA, CrystalB, and CrystalC inside, with Size=3,3,3, and Position respectively -10,3,-10; 0,3,-20; 10,3,-10.
  3. Each crystal Anchored=true, CanCollide=false, CanTouch=true; colors are different.
  4. Create CrystalQuest in ServerScriptService.
Official screenshots of folders and model levels in Explorer
Figure 19-2: Official Explorer hierarchy reference: Demonstration container and sub-object here; the Crystals folder of this lesson is placed under Workspace.Roblox Creator Documentation · CC BY 4.0 · The original image has not been modified · Description of the permit

Step by step

02·First recognize the two progress labels

Crystals saves the collected quantity, QuestComplete saves whether it is completed. SetAttribute sets a value, and GetAttribute reads it; they are different from the built-in Position and Color, and are self-defined labels.

Collection records must be modified by the server. The client can use GetAttributeChangedSignal to update the HUD in the future, but it cannot treat the number it changes as the actual task progress.

  1. Select Player from the server perspective of Play and find Attributes under Properties.
  2. You should initially see Crystals=0, QuestComplete=false.

Step by step

03 · Use "Player → Already Received Crystal" to prevent duplication

collected[player] is the collection table of this player, which uses crystal objects as keys. Touching the same object repeatedly will return immediately, while touching another one will continue processing.

All judgments are made before increasing progress. The distance check allows a range of 8 studs for small hedrons; this range needs to be redesigned when changing to larger objects.

  1. Put the complete code into CrystalQuest.
  2. Touch in the order of A, A, B, C, and the expected progress is 1, 1, 2, 3.
  3. Note that the target quantity TARGET must be consistent with the lesson plan, and decorations are not counted as task items.
ServerScriptService > CrystalQuest · Complete scriptLuau
local Players = game:GetService("Players")
local folder = workspace:WaitForChild("Crystals")
local TARGET = 3
local collected = {}
local function setup(player)
    collected[player] = {}
    player:SetAttribute("Crystals", 0)
    player:SetAttribute("QuestComplete", false)
end
Players.PlayerAdded:Connect(setup)
for _, player in ipairs(Players:GetPlayers()) do setup(player) end
Players.PlayerRemoving:Connect(function(player) collected[player] = nil end)

for _, crystal in ipairs(folder:GetChildren()) do
    if not crystal:IsA("BasePart") then continue end
    crystal.Touched:Connect(function(hit)
        local character = hit:FindFirstAncestorOfClass("Model")
        local player = character and Players:GetPlayerFromCharacter(character)
        local root = character and character:FindFirstChild("HumanoidRootPart")
        local humanoid = character and character:FindFirstChildOfClass("Humanoid")
        if not player or not root or not humanoid or humanoid.Health <= 0 then return end
        if (root.Position - crystal.Position).Magnitude > 8 then return end
        local record = collected[player]
        if not record or record[crystal] then return end
        record[crystal] = true
        local count = (player:GetAttribute("Crystals") or 0) + 1
        player:SetAttribute("Crystals", count)
        player:SetAttribute("QuestComplete", count >= TARGET)
        print(player.Name, "Crystals:", count, "/", TARGET)
    end)
end

Step by step

04 · Visible objects may not still be available for collection

The same crystal may have been claimed by A, but is still available for B, so the shared appearance cannot simply represent everyone's task status.

This lesson uses Output and personal Attribute verification. A personal HUD will be added to the comprehensive project; if you want the received crystals to fade out only for you, you also need the client's personal display logic.

  1. After A touches CrystalA, and B touches the same crystal again, both of them should get 1 each.
  2. If A touches CrystalA again after rebirth, 1 should not be added.
  3. The entire task record will be cleared only after Stop and restart.

Step by step

05 · Check the meaning of "three different ones"

The task is completed not by touching three times, but by touching three different crystal stones. Write this sentence down first, and then check whether the code complies with it.

If new spar are later dynamically created, this connection to GetChildren on startup will not automatically overwrite them. The items in this lesson are placed before starting; dynamic generation requires additional processing of ChildAdded.

  1. At least five consecutive hits on the same one, with progress remaining at 1.
  2. It can still be completed by collecting three in different orders.
  3. Verify that no other scripts are changing Crystals at the same time.

Homework

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

1 Search order freedom

Design three different routes to verify that they can be done regardless of which one is found first.

2 Clear mission statement

Write "Collect three different crystals" to avoid players mistakenly thinking that the same crystal can be brushed three times.

3 Selected: 4 tasks

Add CrystalD and change TARGET to 4, and redo the repeated collection test.

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

Exit questions

Answer it yourself first, and then expand on it.

Are Crystals a built-in property of Roblox?

No, it is a custom attribute defined through SetAttribute.

Why not destroy the crystal directly after receiving it?

This lesson stipulates that everyone has one copy, and server deletion will cause other players to lose it.

What prevents the same one from being counted repeatedly?

collected[player][crystal] records the objects that this player has collected.

Will a new crystal be automatically connected after running?

The code in this lesson does not; it only traverses existing child objects at startup.

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