Skip to content

World

System · World · server and client scripts

World is the top of the place's tree of parts and entities. It also runs queries over scene entities and schedules per-frame systems. The server and each client have their own world, and a call acts on the world of the side that makes it.

A part at the top of the place has World as its Parent, and part.Parent = World moves a part there. Find an entity at the top of the place with World.Find(path), list children with World.Children() or World.Descendants(), and wait for an entity with World.WaitFor(path, seconds).

Children

Function
Children() -> {Entity}

Children returns an array of every top-level entity in the world.

Example

-- Server script.
for _, entity in World.Children() do
    print(entity.Name)
end

Descendants

Function
Descendants() -> {Entity}

Descendants returns an array of every entity in the world, top level first.

Example

-- Server script.
print(#World.Descendants())

Find

Function
Find(path: string) -> Entity?

Find resolves a child name or slash-separated path from the top of the world and returns the matching entity, or nil.

Example

-- Server script.
local gate = World.Find("Arena/Gate")
if gate then
    print(gate.Name)
end

FindDescendant

Function
FindDescendant(name: string) -> Entity?

FindDescendant searches every level of the world for the first entity with the given name and returns it, or nil.

Example

-- Server script.
local beacon = World.FindDescendant("Beacon")
if beacon then
    print(beacon.Position)
end

WaitFor

Function
WaitFor(path: string, seconds: number?) -> Entity?

WaitFor waits until a top-level entity or path exists in the world, or until seconds seconds pass, returning the entity or nil.

Example

-- Server script.
local arena = World.WaitFor("Arena", 5)
if arena then
    print(arena.Name)
end

On

Function
On(signal: any, fn: (...any) -> ()) -> Connection

On connects a handler to a singleton component signal and returns a Connection.

Example

-- Server script.
World.On(Component.Gravity.Changed, function()
    print("gravity updated")
end)

Once

Function
Once(signal: any, fn: (...any) -> ()) -> Connection

Once connects a one-shot handler to a singleton component signal and returns a Connection.

Example

-- Server script.
World.Once(Component.Gravity.Changed, function()
    print("gravity changed once")
end)

Wait

Function
Wait(signal: any, seconds: number?) -> (boolean, ...any)

Wait yields until a singleton component signal fires or seconds pass, returning true and the signal arguments on fire or false on timeout.

Example

-- Server script.
local ok = World.Wait(Component.Gravity.Changed, 5)
print(ok)

Override

Function
Override(target: any, value: any) -> ()

Override sets a client-only override on a singleton presentation component or field without changing replicated state.

Example

-- Client script.
World.Override(Component.Fog.End, 40)

ClearOverride

Function
ClearOverride(target: any) -> ()

ClearOverride removes a local client presentation override on a singleton presentation component or field.

Example

-- Client script.
World.ClearOverride(Component.Fog.End)

GetOverride

Function
GetOverride(target: any) -> any

GetOverride returns the active local client presentation override value on a singleton component or field, or nil if no override is active.

Example

-- Client script.
local fogEnd = World.GetOverride(Component.Fog.End)
print(fogEnd)

Gravity

Property
Gravity: vector

Gravity is the world-space acceleration vector, in units per second squared, that pulls every unanchored body. Negative Y pulls down. A server write replicates to clients. Particles with ParticleGravity.Scale 1 fall at minus Gravity.Y / 20 units per second squared.

Default Vector3.new(0, -196.2, 0)

Example

-- Server script.
World.Gravity = Vector3.new(0, -50, 0)
print(math.round(World.Gravity.Y)) -- prints -50

Clock

PropertyRead only
Clock: { read Tick: number, read ServerTime: number, read TickRate: number, read SessionTime: number }

Clock exposes the simulation tick counter (Tick), fixed tick rate in Hz (TickRate), synchronized server clock in seconds (ServerTime), and local session uptime in seconds (SessionTime).

Example

-- Server script.
print(World.Clock.Tick, World.Clock.TickRate, World.Clock.ServerTime)

Sun

Property
Sun: Sun

Sun is the world's directional sunlight singleton. See Sun for Azimuth, Elevation, Tint, and Intensity.

Ambient

Property
Ambient: Ambient

Ambient is the world's ambient lighting singleton. See Ambient for Source, Tint, Contribution, and Intensity.

Clouds

Property
Clouds: Clouds

Clouds is the world's volumetric cloud layer singleton. See Clouds for cloud coverage, density, height, wind, and shading settings.

Fog

Property
Fog: Fog

Fog is the world's distance fog singleton. See Fog for Color, Mode, Density, Start, and End.

Exposure

Property
Exposure: Exposure

Exposure is the world's automatic exposure singleton. See Exposure for MinEV and MaxEV.

Flare

Property
Flare: Flare

Flare is the world's sun lens flare singleton. See Flare for Intensity, Scale, Tint, Ghost, and Rays.

PlayerSpawner

Property
PlayerSpawner: PlayerSpawner

PlayerSpawner is the world singleton that spawns and respawns player characters. See PlayerSpawner for CharacterPrefab, RespawnDelay, AutoSpawn, and Spawn.

DefineComponent

Function
DefineComponent(name: string, fields: any?, options: ComponentOptions?) -> Component<any>

DefineComponent declares a component and returns its Component handle. Each entry in fields is a default value whose Luau type sets the field type or a Field descriptor: a number defines an f64 field, a boolean a bool, a string a string, a vector (Vector3) a vec3, a Vector2 a vec2, a Color a color, a Rotation a rotation, and a UIVector a UI coordinate. Use Field.Entity, Field.List, Field.Enum, and Field.Optional for entity references, arrays, string enums, and optional values.

A component with no fields is a tag. Reading entity.Enemy returns true while the tag is attached and nil otherwise. A server script can name a tag after DefineComponent declares it or the place file defines it; a client script can name any tag replicated from the server.

options accepts a ComponentOptions table with Scope ("Replicated", "Server", or "Client"), Saved, Singleton, Signals, PlayerList, Exclusive, OnDeleteTarget, and Hints. Each entry in Signals is an array of Field.Arg(name, default) descriptors that creates a DeclaredSignal member on the returned component handle.

Example

-- Server script.
local Stamina = World.DefineComponent("Stamina", {
    Current = 100,
    Max = 100,
    Tint = Color.White,
}, {
    Scope = "Replicated",
    Signals = {
        Drained = { Field.Arg("amount", 0) },
    },
})
local entity = World.Spawn({})
entity:Add(Stamina)
local drained: DeclaredSignal<number> = (Stamina :: any).Drained
drained:Connect(function(e: Entity, amount: number)
    print(e.Name, amount)
end)
drained:Fire(entity, 25)
local Enemy = World.DefineComponent("Enemy")
entity:Add(Enemy)

Queries

Pair

Function
Pair(relation: string, target: Entity | World | string) -> Pair

Pair makes a relationship pair for World.Query, World.Spawn, Entity:Add, and Entity:Has. The relation is "ChildOf", "Tag" or "WornBy", and the target "*" matches any target.

Example

-- Server script in a part.
local Owner = World.DefineComponent("Owner")
local target = World.Spawn({ Shape = "Box" })
self:Add(World.Pair(Owner, target))
print(self:Has(World.Pair(Owner, target))) -- prints true

Query

Function
Query(...string | Pair | ComponentHandle) -> Query

Query builds a cached query over scene entities that have every given component and pair. Column describes the columns returned by query:Column(component).

Example

-- Server script in a part.
local Projectile = World.DefineComponent("Projectile")
self:Add(Projectile)
local bullets = World.Query(Component.Position, Projectile)
local positions = bullets:Column(Component.Position)
for i = 1, #bullets do
    positions[i] += Vector3.new(0, 0, -1)
end
Errors (1)
Error When
World.Query: live query cap reached (256); queries must be created at script load, not per tick Create queries when the script loads, not in a loop or a per-frame callback.

Spawn

Spawn

Function
Spawn(...string | Pair | ComponentHandle) -> Entity

Spawn creates an entity at the top of the place with the given components and tag pairs.

Example

-- Server script.
local Target = World.DefineComponent("Target")
local target = World.Spawn({
    Shape = "Box",
    Climbable = true,
    Position = Vector3.new(0, 4, 0),
})
target:Add(Target)
print(target:IsAlive()) -- prints true
Errors (1)
Error When
World.Spawn: cannot mix 3D and UI components ('<name>') The arguments name both a 3D and a UI component.