Skip to content

Prefabs

System · Prefabs · server and client scripts

Prefabs spawns copies of prefabs, inspects which prefab an entity came from, and resets edited copies back to their prefab defaults. A prefab is a saved entity tree exported to an .ldv file named after the prefab.

Spawn

Function
Spawn(name: string, init: SpawnInit?) -> Entity

Spawn creates a copy of the prefab named name, applies an optional table of root field overrides, and returns its root entity. Raises an error when no prefab with that name exists.

Effects

  • On a client, Spawn spawns only platform prefabs and raises an error for place prefabs, though Exists also finds place prefabs there.

Example

-- Server script.
local block = Prefabs.Spawn("Part", {
    Position = Vector3.new(24, 0, -50),
})
print(block.Name)

Exists

Function
Exists(name: string) -> boolean

Exists returns whether the place or the platform has a prefab named name.

Example

-- Server script.
if Prefabs.Exists("TreasureChest") then
    local chest = Prefabs.Spawn("TreasureChest", {
        Position = Vector3.new(0, 2, 10),
    })
    print(chest.Name)
end

Of

Function
Of(entity: Entity) -> string?

Of returns the prefab name that entity was spawned from, or nil when the entity is not linked to a prefab.

Example

-- Server script.
local crate = Prefabs.Spawn("Part")
local prefabName = Prefabs.Of(crate)
if prefabName then
    crate.Name = prefabName .. "Copy"
end

RevertAddedComponent

Function
RevertAddedComponent(entity: Entity, component: ComponentHandle) -> ()

RevertAddedComponent removes a component handle that a script or Studio added to an entity in a prefab copy. To reset a component that came from the prefab, use RevertEntityOverride.

Example

-- Server script.
local gate = Prefabs.Spawn("Part")
gate:Add(Component.Climbable)
Prefabs.RevertAddedComponent(gate, Component.Climbable)

RevertEntityOverride

Function
RevertEntityOverride(entity: Entity, component: ComponentHandle?) -> ()

RevertEntityOverride resets overridden components on entity back to the prefab's defaults. Pass an optional component handle to revert only that component, or omit it to revert every component shared with the prefab.

Effects

  • On the root of the copy, Name, Position, Rotation, LocalPosition, LocalRotation, and Pivot keep their values.

Example

-- Server script.
local door = Prefabs.Spawn("Part")
door.Size = Vector3.new(10, 10, 10)
Prefabs.RevertEntityOverride(door)