# Protocol: script

The `script` commands a program sends to a running game, as the engine describes them: what each takes, what it answers and what it does. Back to [all commands](../protocol-commands.md).

## script.reload

`script.reload {name: text, source: text} -> {name: text, step: integer, queued: value}` · trusted-project-only

Swaps one script's code while the game runs, keeping the world and the script's state.

| Parameters | Type | Meaning |
| --- | --- | --- |
| `name` | text | The script's name, as game.info lists them. |
| `source` | text | The new source: Luau, or C# for a script whose name ends in .cs. If it does not load, the old code stays, and the error's data has its line and column. |

| Result | Type | Meaning |
| --- | --- | --- |
| `name` | text | The script that was swapped. |
| `step` | integer | How many fixed steps have run. |
| `queued` | value | false: the code is swapped. true: the debugger holds a script stopped, and the code is swapped when that step is over; script.reloads then says whether it loaded. |

## script.state

`script.state {name?: text} -> {step: integer, states: record, kinds: record}` · read, read-only

Reads what scripts keep between steps: each script's `state` table, as it was stored with the world at the end of the last step.

| Parameters | Type | Meaning |
| --- | --- | --- |
| `name?` | text | Only this script. Left out: every script. |

| Result | Type | Meaning |
| --- | --- | --- |
| `step` | integer | How many fixed steps have run. |
| `states` | record | Each script's name with its state. A Luau table with keys 1 to n is a list; any other table is an object, a number key written as text; an empty table is always {}. An entity is the number generation * 4294967296 + index, a whole number below 2^53 (4294967299 is e3.1). A number that is not finite is the text "Infinity", "-Infinity" or "NaN". |
| `kinds` | record | What a kept value is, where it is more than the plain value it reads as: a script's name with {key: kind} for each such key of its state, a key inside a table written with dots ("boss.target"). A key that is not here is a plain value, and a script with none is not here: {} for a game of Luau scripts, whose values say what they are. The one kind is "thing": a C# script's `Thing` field, whose number is an entity as above, or -1 for no thing. |

## script.reloads

`script.reloads {} -> {reloads: [record]}` · read, read-only

Lists the latest script reloads: which worked, which failed and why, and how long each took to reach the screen.

| Result | Type | Meaning |
| --- | --- | --- |
| `reloads` | [record] | Up to 32, oldest first: {number, script, step, source, ok, error, ms_to_step, ms_to_screen, ms_from_save}. source is "file" for a saved file and "command" for script.reload. A reload is finished when ok is false or ms_to_step is a number: a step has run the new code, in that time from the change being noticed. ms_to_screen and ms_from_save (from the save itself) are null until a window has shown a frame that ran the new code, which a hidden or covered window does not do; wait on ms_to_step, not on them. |

## script.complete

`script.complete {file: text, source?: text, line: integer, column: integer} -> {from: integer, items: [record], read: text, columns: text}` · read, read-only

Says what names the engine knows go at a place in a script: inside the quotes of a name passed to the engine (a part, a field of the part before it, a thing, an event, an input action, a sound, a scene, a library) or after `tuning.`, those of this game that begin with what is written. For an editor's completion as a person types, and for an agent writing a script. Reads `source` when given, so a buffer not yet saved is answered; nothing changes.

| Parameters | Type | Meaning |
| --- | --- | --- |
| `file` | text | The script, from the project file's folder: what is read when `source` is left out. |
| `source?` | text | The script's text as it is in the editor now. Left out: the file's. |
| `line` | integer | The line of the place, from 1. |
| `column` | integer | The column of the place, from 1, in UTF-16 code units: the place is before the character there. |

| Result | Type | Meaning |
| --- | --- | --- |
| `from` | integer | The column where the name being written begins: a completion replaces from there to the place. |
| `items` | [record] | One {label, kind, detail, about} per name that fits: label the name, kind part, field, thing, event, action, tuning, sound, scene or library, detail what it is (a part's fields; a field's part, type and default as a script writes it, "Foe.health: whole number = 3"; a tuning value), about one sentence. None where the engine knows no names go. |
| `read` | text | How the answer was read: "text", the scripts' text and the scene and project files, not the values it runs with. A call to the engine through another name (`local f = get; f(e, "Body", "vx")`) is not seen, and a local that shadows an engine function is taken for it. |
| `columns` | text | The unit columns count in: "utf16", UTF-16 code units from 1, as an editor's text model and the Language Server Protocol count them (a letter outside the Basic Multilingual Plane is two). Lines count from 1. |
