Skip to content

Entity properties and components

An entity keeps its data in components, and its properties read and write that data. The components decide what an entity is: a part has transform, mesh and collision components, a light has Light, a sound has Sound, and an on-screen control has 2D layout components. See Entity for every member.

-- Server script in a part.
local lamp = World.Spawn({})
print(lamp.Light) -- prints nil
lamp:Add(Component.Light)
local light = lamp.Light
if light then
    light.Intensity = 2
    light.Range = 20
end
lamp.Parent = self

print(lamp:Has(Component.Light)) -- prints true
print(lamp.Light and lamp.Light.Intensity) -- prints 2

World.Spawn accepts an initial component table; set components and properties before Parent.

Reading and writing components

  • Index an entity with a component name such as lamp.Light or part.Body to read or write that component's view, or nil when the entity does not have the component. See Component views for every view and field.
  • Core entity properties (Name, Parent, Visible, Active, LocalPosition, LocalRotation, and Material) and single-value component fields (Position, Rotation, Size, Color, and Shape) read and write directly on the entity; the optional component fields return nil when the entity lacks that component.
  • Use Entity:Find to look up child entities by name and Entity:Has to test whether an entity has a component.
  • Call Entity:Add or World.Spawn to add components to an entity. A 3D transform (Position, Rotation, Size) requires Component.Position (or Component.Pivot on a model), and 2D interface layout requires Component.Box.
  • Call Entity:Remove to remove a component or tag. Assigning nil to an optional field clears that field without removing its component.

Component names

Built-in component handles are in the Component namespace, such as Component.Body, Component.Light, or Component.Lifetime, and are passed to World.Query, Entity:Add, Entity:Has, and Entity:Remove.

3D world and 2D user interface

An entity is in either the 3D world or the 2D user interface, never both. In the world, Position and Size are Vector3 values in units. In the interface, under World.Viewport.Screen or inside interface containers, layout uses entity.Box (Position, Size), entity.UIOrder (ZIndex, LayoutOrder), and interface views such as entity.UITextSource, entity.UITextStyle, and entity.Background. Mixing world and interface components on one entity fails with one of the interface layout errors.