32. Instances and the Explorer — Homework solutions

Roblox snippets. Paste one into a Script in ServerScriptService and press Play. The .lua files are in exercises/32/homework/solutions/.

Problem 1 — Build a platform

Worked solution.

local part = Instance.new("Part")
part.Name = "Platform"
part.Size = Vector3.new(10, 1, 10)
part.Position = Vector3.new(0, 8, 0)
part.BrickColor = BrickColor.new("Bright green")
part.Anchored = true
part.Parent = workspace

Common mistakes.

  • Forgetting Anchored = true, so the platform falls on Play.

Problem 2 — Set the properties in order

Worked solution.

local part = Instance.new("Part")
part.Size = Vector3.new(4, 4, 4)
part.BrickColor = BrickColor.new("Bright blue")
part.Anchored = true
part.Parent = workspace    -- Parent goes LAST: the Part only becomes
                           -- live in the world once it is parented, so
                           -- set every property before this line.

With Parent last, the Part appears the right size and colour, no flicker.

Problem 3 — Safe find

Worked solution.

local wall = workspace:FindFirstChild("OldWall")
if wall then
    wall:Destroy()
else
    print("nothing to remove")
end

FindFirstChild returns nil when nothing matches, and nil is falsy, so the if handles both cases. workspace.OldWall would error when the wall is missing.

Challenge — A row of pillars

Worked solution.

for i = 1, 5 do
    local pillar = Instance.new("Part")
    pillar.Name = "Pillar" .. i
    pillar.Size = Vector3.new(2, 10, 2)
    pillar.Anchored = true
    pillar.Position = Vector3.new(i * 6, 5, 0)   -- spaced along x
    pillar.BrickColor = BrickColor.new("Dark stone grey")
    pillar.Parent = workspace
end

The counter i does two jobs: it numbers each pillar's name, and i * 6 spreads them along the x axis. Same loop you used for printing — here it builds five Instances.

Done?

You can build, change, find, and remove Instances from code. Next: Events and connections — how your code reacts to things in the world.