Skip to content

Csg

System · Csg · server and client scripts

Csg combines parts into one solid shape, called a union, and takes unions apart again. A union draws one outer surface and collides as one body.

Every function yields until the result and its collider are ready, so it raises an error inside Update, PhysicsUpdate, or a World.System callback. Call it from the script body, task.spawn, or a coroutine. On failure, each function returns nil and an error message.

Intersect

FunctionServer only
Intersect(parts: { Entity }) -> (Entity?, CsgFailure?)

Intersect replaces the parts with one union that keeps only the volume they all share.

Effects

  • Returns nil, "result is empty" when the parts do not overlap.

Example

-- Server script.
local left = World.Spawn({ Shape = "Box" })
if left.Body then left.Body.Motion = "Static" end
left.Position = Vector3.new(0, 5, 0)

local right = World.Spawn({ Shape = "Box" })
if right.Body then right.Body.Motion = "Static" end
right.Position = Vector3.new(2, 5, 0)

-- keep only the overlap of the two boxes
local overlap, err = Csg.Intersect({ left, right })
if not overlap then
    warn("intersect failed: " .. (err or ""))
end
Errors (1)
Error When
Intersect can only be called from the server Called from a client script.

Separate

FunctionServer only
Separate(union: Entity) -> ({ Entity }?, CsgFailure?)

Separate destroys a union or negative entity after moving its children to its parent, and returns the children.

Effects

  • If the union was resized, the stretch is applied to the children's positions and sizes.

Example

-- Server script.
local a = World.Spawn({ Shape = "Box" })
if a.Body then a.Body.Motion = "Static" end
local b = World.Spawn({ Shape = "Box" })
if b.Body then b.Body.Motion = "Static" end
b.Position = Vector3.new(4, 0, 0)

local union = Csg.Union({ a, b })
if union then
    -- break the union back into its parts
    local pieces = Csg.Separate(union)
    if pieces then
        for _, piece in pieces do
            piece.Name = "Debris"
        end
    end
end
Errors (1)
Error When
Separate can only be called from the server Called from a client script.

Subtract

FunctionServer only
Subtract(main: Entity, cutters: { Entity }) -> (Entity?, CsgFailure?)

Subtract cuts cutters out of main and returns one union of what remains, such as a wall with a doorway. The union takes main's parent, rotation, color, material, and anchoring.

Example

-- Server script.
local wall = World.Spawn({ Shape = "Box" })
if wall.Body then wall.Body.Motion = "Static" end
wall.Size = Vector3.new(12, 8, 1)
wall.Position = Vector3.new(0, 4, 0)

local doorway = World.Spawn({ Shape = "Box" })
if doorway.Body then doorway.Body.Motion = "Static" end
doorway.Size = Vector3.new(4, 6, 2)
doorway.Position = Vector3.new(0, 3, 0)

-- cut the doorway out of the wall
local cut, err = Csg.Subtract(wall, { doorway })
if cut then
    cut.Name = "WallWithDoor"
else
    warn("doorway failed: " .. (err or ""))
end
Errors (1)
Error When
Subtract can only be called from the server Called from a client script.

Union

FunctionServer only
Union(parts: { Entity }) -> (Entity?, CsgFailure?)

Union joins parts into one union that draws and collides as a single solid. The parts move inside the union and keep their identity, so existing references to them stay valid.

Effects

  • Returns nil and "union is too complex (<triangles> triangles, limit 20000)" when the surface needs too many triangles; use fewer parts or fewer round parts.
  • Returns nil and "too many operands (limit 4096)" when the call names too many parts.

Example

-- Server script.
local left = World.Spawn({ Shape = "Box" })
if left.Body then left.Body.Motion = "Static" end
left.Size = Vector3.new(1, 6, 1)
left.Position = Vector3.new(-3, 3, 0)

local right = World.Spawn({ Shape = "Box" })
if right.Body then right.Body.Motion = "Static" end
right.Size = Vector3.new(1, 6, 1)
right.Position = Vector3.new(3, 3, 0)

local beam = World.Spawn({ Shape = "Box" })
if beam.Body then beam.Body.Motion = "Static" end
beam.Size = Vector3.new(7, 1, 1)
beam.Position = Vector3.new(0, 6.5, 0)

-- join two posts and a beam into one arch
local arch, err = Csg.Union({ left, right, beam })
if arch then
    arch.Name = "Arch"
else
    warn("arch failed: " .. (err or ""))
end
Errors (1)
Error When
Union can only be called from the server Called from a client script.