‹ Back to Tutorials Beginner

Roblox Luau Tutorial: Understanding the Parent-Child Relationship

Learn how objects interact within Roblox's hierarchy (DataModel / Game) and how to write scripts to modify an object's properties dynamically.


1. What is the Parent-Child Relationship?

Hierarchy Structure

game (DataModel / Parent)
└── Workspace (Child of game / Parent of Part)
    └── Part (Child of Workspace / Parent of Script)
        └── Script (Child of Part)

2. Setting Up the Scene in Roblox Studio

  1. Insert a Part:
    • Press Ctrl + I (Windows) or Cmd + I (Mac) to open the Insert Object menu.
    • Search for Part and select it.
  2. Insert a Script inside the Part:
    • Press Ctrl + I / Cmd + I again while selecting the Part.
    • Search for Script and place it inside the Part.

3. Methods to Access Parents and Children in Luau

Method 1: The Parent Property (Direct Reference)

Since the script is placed directly inside the Part, referencing script.Parent gives direct access to that Part.

-- Accessing the parent directly from the script
local part = script.Parent

Method 2: The Child Method (Path Reference)

Accessing an object by navigating down through the DataModel hierarchy (game.Workspace.Part).

-- Accessing the part via the DataModel path
local part = game.Workspace.Part

Method 3: Function Methods (FindFirstChild & FindFirstAncestor)

Using built-in Roblox functions to search up or down the tree safely.

-- Search upward through parents for an object named "Part"
local part = script:FindFirstAncestor("Part")

-- Search downward inside Workspace for a child named "Part"
local part = workspace:FindFirstChild("Part")

4. Modifying Object Properties via Code

Using script.Parent, you can dynamically change property values such as Name, BrickColor, and Transparency:

-- Reference the Part parented to this script
local part = script.Parent

-- 1. Change the Name property
part.Name = "John Doe"

-- 2. Change the Color property
part.BrickColor = BrickColor.new("Bright green")

-- 3. Change the Transparency property
part.Transparency = 0.5

5. Verification / Testing

  1. Click Run in Roblox Studio.
  2. Inspect the Part in the Workspace:
    • Color: Has changed to bright green.
    • Transparency: Is visually semi-transparent (0.5).
    • Explorer Name: Updated to "John Doe".