Skip to content

Physics

System · Physics · server and client scripts

Physics has the place-wide simulation settings, the collision groups, and the spatial queries against collider shapes. The server and each client step their own physics world, and a call acts on the world of the side that makes it.

To push one part, call Body:ApplyImpulse or set Velocity on it. For when physics runs relative to scripts, see PhysicsUpdate.

Replaying

PropertyRead only
Replaying: boolean

Replaying is true while a predicted client physics step is replaying past inputs after a server correction, and false during normal simulation steps.

Default false

Example

-- Client script.
if not Physics.Replaying then
    print("normal simulation step")
end

Collision groups

RegisterCollisionGroup

FunctionServer only
RegisterCollisionGroup(name: string) -> ()

RegisterCollisionGroup adds a collision group named name. A part joins it when its CollisionGroup is set to that name. A new group collides with every group until SetCollides changes that. A place has 32 group slots, and Default, Character and CharacterNoCollide take three of them.

Example

-- Server script in a part.
Physics.RegisterCollisionGroup("Projectile")
Physics.SetCollides("Projectile", "Projectile", false)
self.CollisionGroup = "Projectile"
print(self.CollisionGroup) -- prints Projectile
Errors (1)
Error When
RegisterCollisionGroup can only be called from the server Called from a client script.

SetCollides

FunctionServer only
SetCollides(a: string, b: string, collides: boolean) -> ()

SetCollides sets whether parts of group a collide with parts of group b. Pass the same group twice to set whether it collides with itself.

Effects

  • Spatial queries follow the rules only when their options name a CollisionGroup.
  • Characters follow their group's rules for walking, standing, stepping, climbing and swimming.

Example

-- Server script.
Physics.RegisterCollisionGroup("Projectiles")
Physics.RegisterCollisionGroup("Debris")
Physics.SetCollides("Projectiles", "Debris", false)
Errors (1)
Error When
SetCollides can only be called from the server Called from a client script.

Collides

Function
Collides(a: string, b: string) -> boolean

Collides returns whether group a and group b are configured to collide.

Example

-- Server script.
Physics.RegisterCollisionGroup("Projectiles")
Physics.SetCollides("Projectiles", "Projectiles", false)
print(Physics.Collides("Projectiles", "Projectiles")) -- prints false

CollisionGroups

Function
CollisionGroups() -> {string}

CollisionGroups returns the list of registered collision group names in slot order.

Example

-- Server script.
for _, name in Physics.CollisionGroups() do
    print(name)
end

Explosion

Explode

FunctionServer only
Explode(center: Vector3, radius: number, impulse: number, options: ExplodeOptions?) -> {Entity}

Explode pushes dynamic bodies and characters away from center within radius units with linear falloff from the closest point on each collider, and returns the pushed root entities nearest first. impulse is the peak impulse at the center; a negative value pulls bodies in. Walls do not block the blast, and joints are exempt from the blast impulse unless options.BreakJoints is true.

Example

-- Server script in a grenade part.
task.wait(3)
local pos = self.Position
if pos then
    local hit = Physics.Explode(pos, 15, 4000, {
        Exclude = { self },
        BreakJoints = true,
    })
    print(#hit)
end
self:Destroy()
Errors (1)
Error When
Explode can only be called from the server Called from a client script.

Perception

EmitSound

FunctionServer only
EmitSound(position: Vector3, radius: number, options: EmitSoundOptions?) -> number

EmitSound emits a sound at position that server listeners with Hearing hear within radius * Sensitivity units and returns how many listeners heard it. Optional options accepts Source (an entity that does not hear itself and provides the collision group) and Tag (a string of 1 to 255 bytes).

Example

-- Server script in a part.
local pos = self.Position
if pos then
    local heard = Physics.EmitSound(pos, 40, { Source = self, Tag = "Footstep" })
    print(heard)
end
Errors (1)
Error When
EmitSound can only be called from the server Called from a client script.

Queries

Raycast

Function
Raycast(origin: Vector3, direction: Vector3, maxDistance: number, options: QueryOptions?) -> RaycastHit?

Raycast returns the first surface hit from origin along dir within maxDist units, or nil. The hit has Position, Normal, Entity, Distance, and LocalPosition and LocalNormal in the frame of the hit Entity.

A client ray tests every character's Head, Torso, arms and legs where that client draws them, so a ray at a character's Head returns that Head on a client as it does on a server. Only a server ray can rewind characters with AtTick.

Example

-- Server script in a part.
local pos = self.Position
if pos then
    local hit = Physics.Raycast(pos, Vector3.new(0, -1, 0), 1000, {
        Exclude = { self },
    })
    if hit and hit.Entity then
        print(hit.Entity.Name, hit.Distance)
    end
end

RaycastAll

Function
RaycastAll(origin: Vector3, direction: Vector3, maxDistance: number, options: QueryOptions?, out: {RaycastHit}?) -> {RaycastHit}

RaycastAll casts a ray from origin along direction up to maxDistance units and returns every hit ordered nearest first. Pass out to reuse a preallocated table.

Example

-- Server script in a part.
local pos = self.Position
if pos then
    local hits = Physics.RaycastAll(pos, Vector3.new(0, -1, 0), 100, {
        Exclude = { self },
    })
    for _, hit in hits do
        if hit.Entity then
            print(hit.Entity.Name, hit.Distance)
        end
    end
end

Spherecast

Function
Spherecast(origin: Vector3, radius: number, direction: Vector3, maxDistance: number, options: QueryOptions?) -> ShapecastHit?

Spherecast sweeps a sphere of radius from origin along direction up to maxDistance units and returns the first contact, or nil.

Example

-- Server script in a part.
local pos = self.Position
if pos then
    local hit = Physics.Spherecast(pos, 1, Vector3.new(0, -1, 0), 20, {
        Exclude = { self },
    })
    if hit then
        print(hit.Distance, hit.Fraction)
    end
end

SpherecastAll

Function
SpherecastAll(origin: Vector3, radius: number, direction: Vector3, maxDistance: number, options: QueryOptions?, out: {ShapecastHit}?) -> {ShapecastHit}

SpherecastAll sweeps a sphere of radius from origin along direction up to maxDistance units and returns every contact nearest first. Pass out to reuse a table.

Example

-- Server script in a part.
local pos = self.Position
if pos then
    local hits = Physics.SpherecastAll(pos, 1, Vector3.new(0, -1, 0), 20, {
        Exclude = { self },
    })
    print(#hits)
end

Boxcast

Function
Boxcast(origin: Vector3, size: Vector3, rotation: Rotation, direction: Vector3, maxDistance: number, options: QueryOptions?) -> ShapecastHit?

Boxcast sweeps an oriented box of full extents size and orientation rotation from origin along direction up to maxDistance units and returns the first contact, or nil.

Example

-- Server script in a part.
local pos = self.Position
if pos then
    local hit = Physics.Boxcast(
        pos,
        Vector3.new(1, 1, 1),
        Rotation.Identity,
        Vector3.new(0, 0, -1),
        10,
        { Exclude = { self } }
    )
    if hit then
        print(hit.Distance)
    end
end

BoxcastAll

Function
BoxcastAll(origin: Vector3, size: Vector3, rotation: Rotation, direction: Vector3, maxDistance: number, options: QueryOptions?, out: {ShapecastHit}?) -> {ShapecastHit}

BoxcastAll sweeps an oriented box of full extents size and orientation rotation from origin along direction up to maxDistance units and returns every contact nearest first. Pass out to reuse a table.

Example

-- Server script in a part.
local pos = self.Position
if pos then
    local hits = Physics.BoxcastAll(
        pos,
        Vector3.new(1, 1, 1),
        Rotation.Identity,
        Vector3.new(0, 0, -1),
        10,
        { Exclude = { self } }
    )
    print(#hits)
end

Capsulecast

Function
Capsulecast(origin: Vector3, radius: number, height: number, rotation: Rotation, direction: Vector3, maxDistance: number, options: QueryOptions?) -> ShapecastHit?

Capsulecast sweeps a capsule of radius, height, and orientation rotation from origin along direction up to maxDistance units and returns the first contact, or nil.

Example

-- Server script in a part.
local pos = self.Position
if pos then
    local hit = Physics.Capsulecast(
        pos,
        0.5,
        2,
        Rotation.Identity,
        Vector3.new(0, 0, -1),
        10,
        { Exclude = { self } }
    )
    if hit then
        print(hit.Distance)
    end
end

CapsulecastAll

Function
CapsulecastAll(origin: Vector3, radius: number, height: number, rotation: Rotation, direction: Vector3, maxDistance: number, options: QueryOptions?, out: {ShapecastHit}?) -> {ShapecastHit}

CapsulecastAll sweeps a capsule of radius, height, and orientation rotation from origin along direction up to maxDistance units and returns every contact nearest first. Pass out to reuse a table.

Example

-- Server script in a part.
local pos = self.Position
if pos then
    local hits = Physics.CapsulecastAll(
        pos,
        0.5,
        2,
        Rotation.Identity,
        Vector3.new(0, 0, -1),
        10,
        { Exclude = { self } }
    )
    print(#hits)
end

OverlapSphere

Function
OverlapSphere(center: Vector3, radius: number, options: QueryOptions?, out: {Entity}?) -> {Entity}

OverlapSphere returns the entities whose shapes overlap a sphere. It fills out when you pass one, so a script can reuse one table.

Example

-- Server script in a part.
local pos = self.Position
if pos then
    local found = {}
    local hits = Physics.OverlapSphere(pos, 10, {
        Exclude = { self },
    }, found)
    for _, entity in hits do
        print(entity.Name)
    end
end

OverlapBox

Function
OverlapBox(center: Vector3, size: Vector3, rotation: Rotation, options: QueryOptions?, out: {Entity}?) -> {Entity}

OverlapBox returns the entities whose shapes overlap an oriented box with full extents size and orientation rotation.

Example

-- Server script in a door part.
local pos, size = self.Position, self.Size
if pos and size then
    local hits = Physics.OverlapBox(pos, size, Rotation.Identity, {
        Exclude = { self },
    })
    print(#hits == 0)
end

OverlapCapsule

Function
OverlapCapsule(center: Vector3, radius: number, height: number, rotation: Rotation, options: QueryOptions?, out: {Entity}?) -> {Entity}

OverlapCapsule returns the entities whose colliders overlap a capsule centered at center with radius, height, and orientation rotation. Pass out to reuse a table.

Example

-- Server script in a part.
local pos = self.Position
if pos then
    local hits = Physics.OverlapCapsule(
        pos,
        0.5,
        2,
        Rotation.Identity,
        { Exclude = { self } }
    )
    print(#hits)
end