Skip to main content

Scope

Cleanup for one stay in a State. Everything added to a scope is cleaned up when the machine leaves that state, however it leaves: a normal transition, an interrupt, a timeout, Machine:ForceState or Machine:Stop.

Get the scope of the state whose callback is running with Machine:GetScope.

attack.OnEnter = function(machine)
	local scope = machine:GetScope()
	scope:Add(hitbox)                       -- hitbox:Destroy() on exit
	scope:Add(track.Stopped:Connect(onEnd)) -- disconnected on exit
	scope:Delay(0.3, function()             -- never runs if the attack is interrupted first
		machine:Send("Recovered")
	end)
end

Properties​

Active​

This item is read only and cannot be modified. Read Only
Scope.Active: boolean

true until the state is left and the scope is cleaned up. A scope is never reused: entering the state again gives it a new one.

Functions​

Add​

Scope:Add(
item: T--

A function, thread, Instance, RBXScriptConnection, or a table with a Destroy or Disconnect method

) → T--

The same item, so it can be added inline

Cleans item up when the state is left: functions are called, threads cancelled, Instances destroyed, connections disconnected, and tables have their Destroy (or Disconnect) method called. Items are cleaned in reverse order of adding. Adding to a scope that was already cleaned cleans the item immediately.

local hitbox = machine:GetScope():Add(Hitbox.new(character))

Delay​

Scope:Delay(
seconds: number,--

Machine time to wait, advanced by Machine:Update

callback: (...any) → (),--

Called with ...

...: any
) → () → ()--

Cancels the timer

Calls callback after seconds of machine time, unless the state is left first. Timers are driven by Machine:Update, so they never fire without it, and fire at the earliest on the next Update.

-- Open the cancel window 0.25s into the attack
machine:GetScope():Delay(0.25, function()
	machine.Context.CanCancel = true
end)
Show raw api
{
    "functions": [
        {
            "name": "Add",
            "desc": "Cleans `item` up when the state is left: functions are called, threads cancelled, Instances destroyed,\nconnections disconnected, and tables have their `Destroy` (or `Disconnect`) method called.\nItems are cleaned in reverse order of adding. Adding to a scope that was already cleaned cleans the item immediately.\n\n```lua\nlocal hitbox = machine:GetScope():Add(Hitbox.new(character))\n```",
            "params": [
                {
                    "name": "item",
                    "desc": "A function, thread, Instance, RBXScriptConnection, or a table with a `Destroy` or `Disconnect` method",
                    "lua_type": "T"
                }
            ],
            "returns": [
                {
                    "desc": "The same item, so it can be added inline",
                    "lua_type": "T"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 91,
                "path": "src/scope.luau"
            }
        },
        {
            "name": "Delay",
            "desc": "Calls `callback` after `seconds` of machine time, unless the state is left first. Timers are driven by\n[Machine:Update], so they never fire without it, and fire at the earliest on the next `Update`.\n\n```lua\n-- Open the cancel window 0.25s into the attack\nmachine:GetScope():Delay(0.25, function()\n\tmachine.Context.CanCancel = true\nend)\n```",
            "params": [
                {
                    "name": "seconds",
                    "desc": "Machine time to wait, advanced by [Machine:Update]",
                    "lua_type": "number"
                },
                {
                    "name": "callback",
                    "desc": "Called with `...`",
                    "lua_type": "(...any) -> ()"
                },
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "Cancels the timer",
                    "lua_type": "() -> ()"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 123,
                "path": "src/scope.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "Active",
            "desc": "`true` until the state is left and the scope is cleaned up. A scope is never reused: entering the state\nagain gives it a new one.",
            "lua_type": "boolean",
            "readonly": true,
            "source": {
                "line": 33,
                "path": "src/scope.luau"
            }
        }
    ],
    "types": [],
    "name": "Scope",
    "desc": "Cleanup for one stay in a [State]. Everything added to a scope is cleaned up when the machine leaves that\nstate, however it leaves: a normal transition, an interrupt, a timeout, [Machine:ForceState] or [Machine:Stop].\n\nGet the scope of the state whose callback is running with [Machine:GetScope].\n\n```lua\nattack.OnEnter = function(machine)\n\tlocal scope = machine:GetScope()\n\tscope:Add(hitbox)                       -- hitbox:Destroy() on exit\n\tscope:Add(track.Stopped:Connect(onEnd)) -- disconnected on exit\n\tscope:Delay(0.3, function()             -- never runs if the attack is interrupted first\n\t\tmachine:Send(\"Recovered\")\n\tend)\nend\n```",
    "source": {
        "line": 25,
        "path": "src/scope.luau"
    }
}