> For the complete documentation index, see [llms.txt](https://luadoc.edgetx.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://luadoc.edgetx.org/2.9/part_i_-_script_type_overview/function_scripts.md).

# Function Scripts

## Overview

Function scripts are invoked via the **'Lua Script'** option of Special Functions configuration page.

Typical uses of Function scripts are:

* specialized handling in response to switch position changes
* customized announcements

Please be aware that:

* all function scripts are stopped if a One-Time Lua script is running
* Function scripts **DO NOT HAVE ACCESS TO LCD DISPLAY**

## Lifetime

* `init` function is called once when the model is selected
* depending on the switch associated with the Special Function, either the `run` function (switch = on) or the `background` function (switch = off) is called periodically
* the script is stopped and disabled if it misbehaves (e.g. run-time error or low memory)

## File Location

Scripts are located on the SD card in the folder /SCRIPTS/FUNCTIONS/<*name*>.lua. File name length (without extension) **must be 6 characters or less** (this limit was 8 characters in OpenTX 2.1).

## Interface

Every script must include a `return` statement at the end, defining its interface to EdgeTX. This statement returns a table with the following fields:

* `init` function (optional)
* `run` function
* `background` function (optional)

### Example

```lua
local function init()
  -- Called once when the script is loaded
end

local function run()
  -- Called periodically while the Special Function switch is on
end

local function background()
  -- Called periodically while the Special Function switch is off
end

return { run=run, background=background, init=init }
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://luadoc.edgetx.org/2.9/part_i_-_script_type_overview/function_scripts.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
