> For the complete documentation index, see [llms.txt](https://docs.xeutrino.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.xeutrino.com/examples/worked-example.md).

# Complete worked example

A self-contained script that builds a small UI, polls every frame, reads and writes a script global, and triggers features from buttons.

```lua
-- Drop this file at /data/Nexus/Scripts/example.lua and start it from the menu.

-- 1. Build the UI (runs once at script start)
add_subtitle("Example Script by Nexus")
add_checkbox("God Mode",        "chk_god")
add_checkbox("Rainbow HUD",     "chk_rainbow")
add_int_option ("Cash Multiplier", "int_mult", 1, 1, 10, 1)
add_button("Give $1M",          "btn_money")
add_button("Nuke Lobby",        "btn_nuke")

-- 2. Main loop
while true do
    local ped = GET_PLAYER_PED(-1)

    -- Checkbox-driven feature
    SET_ENTITY_INVINCIBLE(ped, get_opt("chk_god") == 1)

    -- Read a global that the game itself uses, modify it back
    -- (illustrative id; replace with a real one for your build)
    local wave = get_global_int(0x4042)
    if get_opt("chk_rainbow") == 1 and wave ~= 0 then
        set_global_float(0x4043, (GET_FRAME_COUNT() % 360) / 360.0)
    end

    -- Money button (self-resetting)
    if get_opt("btn_money") == 1 then
        set_opt("btn_money", 0)
        local mult = get_opt("int_mult")
        notify(string.format("Granting 1M x%d", mult))
        -- (your money-native logic here)
    end

    -- Destructive button (self-resetting)
    -- Targets the player selected in the native Player Options menu.
    if get_opt("btn_nuke") == 1 then
        set_opt("btn_nuke", 0)
        local sp = get_selected_player()
        if sp < 0 or sp >= 32 then
            notify("No valid player selected!")
        else
            notify("Nuking!")
            local targetPed = GET_PLAYER_PED(sp)
            local coords = GET_ENTITY_COORDS(targetPed, true)
            ADD_EXPLOSION(coords.x, coords.y, coords.z, 0, 1.0, true, false, 0.0, false)
        end
    end

    -- Mandatory yield so we don't burn the instruction budget
    script.yield(0)
end
```

## What each part does

| Part                                  | Purpose                                                            |
| ------------------------------------- | ------------------------------------------------------------------ |
| `add_subtitle` / `add_*`              | Build the menu tree before the loop starts.                        |
| `get_opt` / `set_opt`                 | Read option state each frame; reset buttons after handling.        |
| `get_selected_player`                 | Read the menu's selected player index (0–31).                      |
| `SET_ENTITY_INVINCIBLE`               | A bare GTA V native call.                                          |
| `get_global_int` / `set_global_float` | Read and modify a script global.                                   |
| `notify`                              | Toast messages.                                                    |
| `script.yield(0)`                     | Let the game run one frame; keeps you under the instruction limit. |


---

# 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://docs.xeutrino.com/examples/worked-example.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.
