Draft preview · this lesson is still being technically validated. Browser experiments and reference material remain available; trusted completion is enabled after publication.
v2 · Activity 4 of 4 · lifecycle · cleanup · idempotency · 28 min Requires Roblox Studio
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.
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.
| Input | Expected |
|---|
local active = true
return function()
if not active then return end
active = false
connection:Disconnect()
endThe first cleanup changes state and disconnects. The second cleanup returns immediately.
New syntax: lifecycle, cleanup, idempotency
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.
Workspace/LampA (Part, tag WorkshopLamp, child ProximityPrompt) ServerScriptService/InspectBehavior (ModuleScript) ServerScriptService/InspectController (Script, RunContext=Server)
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 InspectBehaviorController 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) endIf 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.
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.
The controller stores cleanup functions by object. Removing a tag and destroying an object are lifecycle events; leaving the client does not automatically express every behavior cleanup policy.
Replace this code with the starter? Download your draft to keep a copy.
Edit and run these files in Roblox Studio. This browser has no Roblox services, physics, or replication.
Your output will appear here.
Sign in with RoCreators to save private progress across devices. Community verification is separate.