32. Instances and the Explorer
Chapter 31 showed that a Roblox experience is a tree of
Instances. This chapter zooms in: what an Instance is,
how to see it in the Explorer, and how to make and
change one from code -- all in a Studio Script.
The Explorer is the tree
Open Studio with the Baseplate template. The Explorer panel (top right) shows the experience as an expandable tree:
game
├─ Workspace
│ ├─ Baseplate
│ └─ SpawnLocation
├─ Players
├─ ServerScriptService
└─ ... more services
Every line is an Instance: a Part, Folder, Script,
or service. Each has a ClassName (its kind, like
Part), a Name shown in the Explorer,
properties, and a Parent -- the Instance it lives
inside.
Making an Instance from code
Instance.new("ClassName") creates an Instance. It is not
in the world yet -- it sits alone until you give it a parent:
local part = Instance.new("Part")
part.Name = "Platform"
part.Size = Vector3.new(8, 1, 8)
part.Position = Vector3.new(0, 5, 0)
part.Anchored = true
part.Parent = workspaceMake a Part, set its name, size, and position, anchor it against
gravity, then place it in workspace. Setting
Parent makes it appear in the world and the Explorer.
Vector3.new(x, y, z) is a 3D value for sizes and
positions: x is left/right, y up/down,
z forward/back.
Set the properties before Parent. Once
parented a Part is live; setting size and colour first avoids a flicker
where it looks wrong.
Properties worth knowing
A Part has many properties. The ones you reach for
first:
| Property | What it controls |
|---|---|
Name |
The label in the Explorer. |
Size |
A Vector3 — how big it is. |
Position |
A Vector3 — where its centre is. |
Anchored |
true = ignores gravity and stays put. |
BrickColor |
Its colour, e.g. BrickColor.new("Bright red"). |
Transparency |
0 = solid, 1 = invisible. |
CanCollide |
true = solid to players; false = walk
through. |
part.BrickColor = BrickColor.new("Bright yellow")
part.Transparency = 0
part.CanCollide = trueWalking the tree from code
Find Instances the way you read the Explorer -- walk down from
game or workspace:
workspace.Platform-- the child namedPlatform(errors if missing).workspace:FindFirstChild("Platform")-- the same, but returnsnilwhen missing instead of erroring -- safer to test withif.instance:GetChildren()-- a list of direct children, to loop over withipairs.instance:Destroy()-- removes an Instance for good.instance:Clone()-- makes a copy you can re-parent.
local coin = workspace:FindFirstChild("Coin")
if coin then
coin:Destroy()
endThese use the colon because they are methods on the Instance -- the
obj:method() form from Chapter 26.
Homework
These do not run in a terminal. Treat them as written exercises:
write the code, then check the solutions page. To test for real, paste
each into a Script under ServerScriptService
and Play.
Problem 1 — Build a platform
Open exercises/32/homework/01-platform.lua. Create an
anchored Part named Platform, size 10, 1, 10,
at 0, 8, 0, coloured Bright green, parented to
workspace.
Problem 2 — Set the properties in order
Open exercises/32/homework/02-order.lua. The snippet
sets Parent first, size and colour after. Reorder so every
property is set before Parent, and add a one-line
comment explaining why.
Problem 3 — Safe find
Open exercises/32/homework/03-safe-find.lua. Using
FindFirstChild and an if, destroy a Part named
OldWall in workspace only if it exists;
otherwise print nothing to remove.
Challenge — A row of pillars
Open exercises/32/homework/04-pillars.lua. Using a
for loop, create five anchored pillars (tall thin Parts) in
a row, a few studs apart along the x axis. Set each
pillar's Position from the loop counter.
Stuck or finished? Open the homework solutions page.