# Scripts (https://docs.palpable.technology/docs/developers/scripts)

A script is a TypeScript program that runs on the board. PAL writes it when it builds a project. To read it, open the project's **Tweak** tab and press **View full code** in the Code box; **Debug** next to it shows the script's log. The code is read-only there: to change it, ask PAL.

Every function is listed in the [script reference](/docs/developers/script-reference).

Scripts run on the board itself, so they keep running when the internet is down.

## Key ideas [#key-ideas]

Everything a script does goes through one object, `p`.

```ts
// Read a part's latest value. Empty until the first reading lands, so give a default.
const t = p.read('modulino-thermo', 'temperature') ?? 0;

// Send a part one of its own commands.
p.write('qwiic-relay', 'on');

// Run on a fixed beat. Returns a function that cancels it.
p.every(1000, () => {
  p.setVariable('temperature', p.read('modulino-thermo', 'temperature') ?? 0);
});

// React to a variable that an app or a view sets.
p.onVariableChange('waterNow', (v) => {
  if (v) p.write('qwiic-relay', 'on');
});

// Ask a cloud service. It is async: always await it.
const result = await p.connector(connectorId, toolName, params);

// Keep the script running. Timers and listeners go above this line.
await p.waitUntilStopped();
```

- **`p.read(moduleId, capability)`** returns that capability's latest value. `p.read(moduleId)` returns an object keyed by capability name. Both are empty until the first reading arrives.
- **`p.write(moduleId, command, params?)`** sends one of the part's own commands, with the parameters the part defines.
- **`p.every(ms, callback)`** runs `callback` on a fixed grid. The callback is synchronous. If it runs late, missed ticks are dropped, not queued.
- **`p.setVariable`, `p.getVariable`, `p.onVariableChange`** share values with views, apps and the [project API](/docs/developers/project-api).
- **`p.connector(connectorId, toolName, params?)`** returns a Promise. **Await it.** Without `await` you hold a pending Promise and every field reads `undefined`. It resolves within 30 seconds, to `{}` if the call fails.
- **`p.config`** holds the script's settings, the values you can change in the app without changing code.
- **`await p.waitUntilStopped()`** at the end keeps the script alive. A timer or listener registered after it never runs.

The `p.connector` line shows the shape only. Which connectors and tools a project can use depends on what is turned on for it in the app.

## Not every board can do everything [#not-every-board-can-do-everything]

A script can only use what its board has. On **ESP32 boards**:

- The camera, chime, synth, and LED matrix and pixel frames are not available yet. Calling them throws `not available on this board yet`.
- `p.audio`, `p.midi` and `p.mqtt` write to the script's log that the board has no speaker, no network MIDI or no MQTT client.
- Script variables are not kept across a restart. A restarted script starts from its first line.