Skip to main content

State

This was deprecated in v1.0.0
Part of the deprecated StateManager package; no longer maintained.

One named state of a StateManager. Create one with StateManager.newState(name) (or require a ModuleScript that returns one, see LoadStates), attach callbacks with OnEnter / OnExit, and pass it in the States list of StateManager.new. You can also get one back from StateManager:GetState.

A State owns a Trove that is cleaned each time the state is exited, so use it for anything that should only live while the state is active.

local Round = StateManager.newState("Round")

Round:OnEnter(function(manager, trove, lastState)
	local timer = trove:Add(Instance.new("Sound")) -- destroyed on exit
end)

This package is no longer maintained; see the deprecation notice on StateManager.

Properties​

Name​

This item is read only and cannot be modified. Read Only
State.Name: string

The name given to newState. StateManager:GetState and ChangeState look states up by it.

Entered​

This item is read only and cannot be modified. Read Only
State.Entered: Signal<StateManager,Trove,string?>

Fires (deferred) after this state becomes current. Receives the StateManager, this state's Trove and the previous state's name (or nil). OnEnter connects to it.

Exited​

This item is read only and cannot be modified. Read Only
State.Exited: Signal<>

Fires with no arguments right after this state is exited and its Trove has been cleaned. OnExit connects to it.

Trove​

State.Trove: Trove

Cleaned every time the state is exited (and when the StateManager is destroyed). The same Trove is passed to OnEnter callbacks.

Functions​

OnEnter​

State:OnEnter(
func: (
stateManager: StateManager,
trove: Trove,
lastState: string?
) → ()--

Called each time the state is entered.

) → Connection--

The signal connection; disconnect it to stop listening.

Connects func to this state's Entered signal. It runs (deferred with task.defer, after StateManager.StateChanged has fired) every time the state becomes current, receiving:

  1. stateManager - the StateManager that switched to this state;
  2. trove - this state's Trove, cleaned when the state is exited; connect and create everything state-specific through it;
  3. lastState - the name of the state that was current before, or nil if there was none.

OnExit​

State:OnExit(
func: () → ()--

Called each time the state is exited.

) → Connection--

The signal connection; disconnect it to stop listening.

Connects func to this state's Exited signal. It runs synchronously, with no arguments, every time the state stops being current (from ChangeState or ExitCurrent), after the state's Trove has already been cleaned.

Show raw api
{
    "functions": [
        {
            "name": "OnEnter",
            "desc": "Connects `func` to this state's `Entered` signal. It runs (deferred with `task.defer`, after\n`StateManager.StateChanged` has fired) every time the state becomes current, receiving:\n\n1. `stateManager` - the StateManager that switched to this state;\n2. `trove` - this state's `Trove`, cleaned when the state is exited; connect and create\n   everything state-specific through it;\n3. `lastState` - the name of the state that was current before, or `nil` if there was none.",
            "params": [
                {
                    "name": "func",
                    "desc": "Called each time the state is entered.",
                    "lua_type": "(stateManager: StateManager, trove: Trove, lastState: string?) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "The signal connection; disconnect it to stop listening.",
                    "lua_type": "Connection"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 173,
                "path": "packages/src/StateManager/init.luau"
            }
        },
        {
            "name": "OnExit",
            "desc": "Connects `func` to this state's `Exited` signal. It runs synchronously, with no arguments,\nevery time the state stops being current (from `ChangeState` or `ExitCurrent`), after the\nstate's `Trove` has already been cleaned.",
            "params": [
                {
                    "name": "func",
                    "desc": "Called each time the state is exited.",
                    "lua_type": "() -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "The signal connection; disconnect it to stop listening.",
                    "lua_type": "Connection"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 187,
                "path": "packages/src/StateManager/init.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "Name",
            "desc": "The name given to `newState`. `StateManager:GetState` and `ChangeState` look states up by it.",
            "lua_type": "string",
            "readonly": true,
            "source": {
                "line": 102,
                "path": "packages/src/StateManager/init.luau"
            }
        },
        {
            "name": "Entered",
            "desc": "Fires (deferred) after this state becomes current. Receives the StateManager, this state's `Trove` and the previous state's name (or nil). `OnEnter` connects to it.",
            "lua_type": "Signal<StateManager, Trove, string?>",
            "readonly": true,
            "source": {
                "line": 108,
                "path": "packages/src/StateManager/init.luau"
            }
        },
        {
            "name": "Exited",
            "desc": "Fires with no arguments right after this state is exited and its `Trove` has been cleaned. `OnExit` connects to it.",
            "lua_type": "Signal<>",
            "readonly": true,
            "source": {
                "line": 114,
                "path": "packages/src/StateManager/init.luau"
            }
        },
        {
            "name": "Trove",
            "desc": "Cleaned every time the state is exited (and when the StateManager is destroyed). The same Trove is passed to `OnEnter` callbacks.",
            "lua_type": "Trove",
            "source": {
                "line": 119,
                "path": "packages/src/StateManager/init.luau"
            }
        }
    ],
    "types": [],
    "name": "State",
    "desc": "One named state of a [StateManager](/api/StateManager). Create one with\n`StateManager.newState(name)` (or `require` a ModuleScript that returns one, see\n[LoadStates](/api/StateManager#LoadStates)), attach callbacks with `OnEnter` / `OnExit`, and\npass it in the `States` list of `StateManager.new`. You can also get one back from\n`StateManager:GetState`.\n\nA State owns a `Trove` that is cleaned each time the state is exited, so use it for anything\nthat should only live while the state is active.\n\n```lua\nlocal Round = StateManager.newState(\"Round\")\n\nRound:OnEnter(function(manager, trove, lastState)\n\tlocal timer = trove:Add(Instance.new(\"Sound\")) -- destroyed on exit\nend)\n```\n\nThis package is no longer maintained; see the deprecation notice on\n[StateManager](/api/StateManager).",
    "deprecated": {
        "version": "v1.0.0",
        "desc": "Part of the deprecated StateManager package; no longer maintained."
    },
    "source": {
        "line": 93,
        "path": "packages/src/StateManager/init.luau"
    }
}