Draft preview · this lesson is still being technically validated. Browser experiments and reference material remain available; trusted completion is enabled after publication.

← Back to journey

Give a prop a start and stop lifecycle

v2 · Activity 1 of 0 · lifecycle · cleanup · idempotency · 28 min Requires Roblox Studio

Chapter outline
Loading draft…
Mini-mission

An attachment owns the resources it creates: connections, tweens, and temporary tables. Its cleanup function releases those resources.

Idempotent cleanup can be called twice safely. A simple active flag prevents repeated teardown and tells callbacks the attachment is finished.

Your task

Create InspectBehavior.attach(part) that connects one prompt log and returns cleanup. A controller attaches tagged WorkshopLamp props and calls cleanup when the tag is removed.

Interface: Complete the documented Studio hierarchy and self-check the behavior outside the browser.

InputExpected
Syntax in this activity

New syntax: lifecycle, cleanup, idempotency

Earlier ideas used here
  • collectionservice
  • tags
Self-check rubric
  • One activation logs once.
  • Tag removal ends behavior; tag restoration adds one fresh behavior.
  • A direct controlled double cleanup call is harmless.

Your explanation is a self-check. Exact wording is not automatically graded.

Self-checked Studio exercise. A saved checklist records your own review; no uploaded place file or screenshot is executed here.

Build this hierarchy

Workspace/LampA (Part, tag WorkshopLamp, child ProximityPrompt)
ServerScriptService/InspectBehavior (ModuleScript)
ServerScriptService/InspectController (Script, RunContext=Server)
  1. Build one tagged anchored Part with a ProximityPrompt.
  2. Create the two files as the stated script classes.
ServerScriptService/InspectBehavior.luau

Module owns its event resource.

local InspectBehavior = {}
function InspectBehavior.attach(part)
    local prompt = part:FindFirstChildOfClass("ProximityPrompt")
    assert(prompt, "Add a ProximityPrompt")
    local active = true
    local connection = prompt.Triggered:Connect(function(player)
        if active then print("Inspect", part.Name, player.Name) end
    end)
    return function()
        if not active then return end
        active = false
        connection:Disconnect()
    end
end
return InspectBehavior
ServerScriptService/InspectController.server.luau

Controller releases resources when membership ends.

local CollectionService = game:GetService("CollectionService")
local Behavior = require(script.Parent.InspectBehavior)
local cleanups = {}
local function attach(part)
    if cleanups[part] or not part:IsA("BasePart") then return end
    cleanups[part] = Behavior.attach(part)
end
local function detach(part)
    if cleanups[part] then cleanups[part](); cleanups[part] = nil end
end
CollectionService:GetInstanceAddedSignal("WorkshopLamp"):Connect(attach)
CollectionService:GetInstanceRemovedSignal("WorkshopLamp"):Connect(detach)
for _, part in ipairs(CollectionService:GetTagged("WorkshopLamp")) do attach(part) end

Expected output

  • A prompt logs Inspect, the part name and player name.
  • Removing the tag stops future inspection logs.

Studio assessment checklist

Multiplayer tests

  1. Run a local server and at least one client; compare server and client context where the activity uses both.
Troubleshooting and production considerations

If reattachment logs twice, the old connection was not released.

Calling Disconnect on the signal itself is different from disconnecting the returned connection.

This is a bounded learning lab. It has not been executed in an approved Roblox Studio environment by this website.

A checked checklist records your own review, not automated project competence or a community verification.

Your goal

Create InspectBehavior.attach(part) that connects one prompt log and returns cleanup. A controller attaches tagged WorkshopLamp props and calls cleanup when the tag is removed.

Reset starter

Replace this code with the starter? Download your draft to keep a copy.

Code

Studio source file
Luau keyword highlighting · Ctrl/⌘ + Enter: Run · Esc: leave editor

Edit and run these files in Roblox Studio. This browser has no Roblox services, physics, or replication.

Your output will appear here.

Ready