Open Asterune
Your first script

Your first script

Create a part, set properties, and connect an event handler.

A server Script can create and modify parts in workspace. Add a Script to workspace in an editable place before running these examples. Getting started with Studio describes how to insert the script and run a playtest.

Create a part

Call Instance.new with a creatable class name. Configure the instance before parenting it into the hierarchy.

Luau
local part = Instance.new("Part")
part.Name = "ExamplePart"
part.Size = Vector3.new(4, 1, 4)
part.Position = Vector3.new(0, 5, 0)
part.Anchored = true
part.Parent = workspace

Anchored prevents physics from moving the part. Position specifies the center in studs. Size specifies dimensions along the part's local axes.

Read and change properties

Properties are accessed with a dot. Many values use engine data types or enums.

Luau
local part = workspace:FindFirstChild("ExamplePart")
if part then
  print(part.Position)
  part.Color = Color3.fromRGB(90, 220, 120)
  part.Material = Enum.Material.SmoothPlastic
end

FindFirstChild returns nil when the child is absent. Check the result before reading or assigning its properties.

Connect an event

Connect registers a function that runs when an event fires. Touched passes the contacting part.

Luau
local part = workspace:FindFirstChild("ExamplePart")
if part then
  part.Touched:Connect(function(other)
    print(other.Name)
  end)
end

A character has several body parts, so one character can produce multiple contact events. Add a guard or cooldown before using contact events to award items or apply damage.