> For the complete documentation index, see [llms.txt](https://docs.otland.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.otland.net/lua-guide/fundamentals-1/scopes.md).

# Scopes

Scopes are what define the range of functionality to a variable or function. This specifically applies in Lua to `local` variables & functions. When you define a variable or function with `local`, its scope is defined by the innermost block of code you define it in.

Example of various scopes:

```lua
local x = 15 -- scope is script-wide, x can be accessed anywhere inside of this script

function a()
    local first = true -- first can only be accessed throughout a()'s code block
    if first then
        local second = 123 -- can only be accessed within this if statement
        print(second) -- Output: 123
    end
    print(second) -- nil, cannot access that variable anymore
    print(first) -- Ouput: true | we can access this variable since we're still inside of the function's code block
end
```

Another example showing a function's scope:

```lua
local function outputTable(t)
    local function output(t)
        for k, v in pairs(t) do
            print(k, v)
        end
    end
    output(t) -- output function is only accessible inside of outputTable
end

outputTable({1, 2, 3}) -- works, outputTable's scope is script-wide
output({1, 2, 3}) -- will return an error, output is inaccessible since it's not inside the current scope, it is inside of outputTable. 
```


---

# 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.otland.net/lua-guide/fundamentals-1/scopes.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.
