Skip to content

Plugin

Type

Plugin is the type of the plugin global, which registers a Studio plugin's operators, viewport tools, and Plugins tab buttons. plugin is the Studio plugin whose init.luau or init.lua is running, and nil in every other script.

Studio runs init.luau, or init.lua when the folder has no init.luau, with the server script API on the server of the place you edit. A folder with both files does not load. Call Operator, Tool and Ribbon while that script runs; if it raises an error or waits, Studio disables the plugin. Declare each operator before any tool or ribbon group that references its Id.

Plugin code changes the place only through ctx.Edit in an operator's Execute, so Undo takes back each run as one step.

Operator

Method
Operator(spec: OperatorSpec) -> ()

Operator registers an undoable Studio action exposed as plugin.<Id>.

Example

-- Plugin script (plugins/build/init.luau).
plugin:Operator({
    Id = "build.flatten",
    Label = "Flatten",
    Params = {
        { Name = "Parts", Type = "instances", Hidden = true, From = "Selection" },
    },
    Execute = function(ctx: PluginContext, params)
        local edit = ctx.Edit :: PluginEdit
        for _, part: Entity in params.Parts do
            local size, pos = part.Size, part.Position
            if size and pos then
                edit:Reshape(part, Vector3.new(size.X, 1, size.Z), pos)
            end
        end
        return true
    end,
})

Tool

Method
Tool(spec: ToolSpec) -> ()

Tool registers an interactive 3D viewport tool whose confirmed gesture runs a declared operator. "Escape" is always sent to Modal even when Keys omits it.

Example

-- Plugin script (plugins/build/init.luau).
plugin:Operator({
    Id = "build.raise",
    Label = "Raise",
    Params = { { Name = "Part", Type = "instance", Hidden = true } },
    Execute = function(ctx: PluginContext, params)
        local edit = ctx.Edit :: PluginEdit
        local part = params.Part :: Entity
        local pos = part.Position
        if pos then
            edit:Set(part, "Position", pos + Vector3.new(0, 1, 0))
        end
        return true
    end,
})

plugin:Tool({
    Id = "build.pick",
    Label = "Pick",
    Operator = "build.raise",
    Modal = function(ctx: PluginContext, event)
        if event.Kind == "key" and event.Key == "Escape" then
            return "exit"
        end
        local hit = event.Hit
        if event.Kind == "press" and event.Button == 1 and hit then
            return "confirm", nil, nil, { Part = hit.Part }
        end
        return "running", nil, "Click a part to raise it"
    end,
})

Ribbon

Method
Ribbon(spec: RibbonSpec) -> ()

Ribbon adds a button group of declared operators and tools to Studio's Plugins tab.

Example

-- Plugin script (plugins/build/init.luau).
plugin:Operator({
    Id = "build.mark",
    Label = "Mark",
    Execute = function(ctx: PluginContext)
        return true
    end,
})

plugin:Ribbon({ Group = "Build", Items = { "build.mark" } })