> ## Documentation Index
> Fetch the complete documentation index at: https://docs.leaf7.fun/llms.txt
> Use this file to discover all available pages before exploring further.

# Script Macros

> Access user data inside your Lua scripts with L7 runtime macros.

# L7 Script Macros

Leaf7 injects runtime macros into your script environment so you can access user-specific data like Discord IDs, execution counts, and key expiry — all without any API calls from the client.

<Note>
  Macros are **environment-scoped**. They only exist during your script's execution and are immediately wiped afterwards. Other scripts running in the same executor **cannot** read your macro values.
</Note>

## Available macros

Use these variables directly in your Lua source code — no setup required. Leaf7 detects which ones you reference and injects only those.

| Macro                | Type            | Description                                                      |
| -------------------- | --------------- | ---------------------------------------------------------------- |
| `L7_LinkedDiscordID` | String          | Discord ID of the user executing the script                      |
| `L7_ScriptName`      | String          | Name of your project as set in the dashboard                     |
| `L7_ScriptVersion`   | String          | Current version of your project (e.g. `"1.2.3"`)                 |
| `L7_TotalExecutions` | Number          | How many times this user has executed the script                 |
| `L7_SecondsLeft`     | String / Number | Seconds until key expires, or `"Lifetime"` for non-expiring keys |
| `L7_UserNote`        | String          | Custom note set on the key (or `"FFA"` for free-for-all)         |

## Usage example

Simply reference the macro names in your source code. No imports, no tables, no setup:

```lua theme={null}
print("Welcome!")
print("Your Discord ID: " .. L7_LinkedDiscordID)
print("Script: " .. L7_ScriptName .. " v" .. L7_ScriptVersion)
print("Total executions: " .. tostring(L7_TotalExecutions))
print("Time left: " .. tostring(L7_SecondsLeft))
print("Note: " .. L7_UserNote)
```

**Output for a user with a 30-day key:**

```
Welcome!
Your Discord ID: 123456789012345678
Script: Premium Hub v1.2.3
Total executions: 47
Time left: 2592000
Note: VIP Customer
```

**Output for a lifetime FFA user:**

```
Welcome!
Your Discord ID: 
Script: Premium Hub v1.2.3
Total executions: 1
Time left: Lifetime
Note: FFA
```

## Practical examples

### Welcome screen with key info

```lua theme={null}
local Players = game:GetService("Players")
local player = Players.LocalPlayer

-- Build a personalized greeting
local timeLeft = L7_SecondsLeft
local timeStr

if timeLeft == "Lifetime" then
    timeStr = "♾️ Lifetime"
else
    local days = math.floor(timeLeft / 86400)
    local hours = math.floor((timeLeft % 86400) / 3600)
    timeStr = days .. "d " .. hours .. "h remaining"
end

-- Send to Roblox chat or UI
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━")
print("🍃 " .. L7_ScriptName .. " v" .. L7_ScriptVersion)
print("👤 " .. player.Name)
print("⏰ " .. timeStr)
print("📊 Execution #" .. tostring(L7_TotalExecutions))
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━")
```

### Discord ID gate for premium features

```lua theme={null}
local OWNER_IDS = {
    ["123456789012345678"] = true,
    ["987654321098765432"] = true,
}

if OWNER_IDS[L7_LinkedDiscordID] then
    print("🔓 Owner features unlocked!")
    -- Load owner-only modules
else
    print("🔒 Standard access")
end
```

### Auto-expire warning

```lua theme={null}
if L7_SecondsLeft ~= "Lifetime" then
    if L7_SecondsLeft < 86400 then  -- Less than 1 day
        warn("⚠️ Your key expires in less than 24 hours!")
    elseif L7_SecondsLeft < 259200 then  -- Less than 3 days
        warn("⏰ Your key expires in " .. math.floor(L7_SecondsLeft / 86400) .. " days")
    end
end
```

## How it works (technical)

<Info>
  **Detection:** When you upload your source code, Leaf7 scans for any references to `L7_` macro names. Only referenced macros are injected — zero overhead for unused ones.
</Info>

<Info>
  **Injection:** On each execution, Leaf7 sets the macro values in `getgenv()` before your script runs, then immediately **nils them out** after your script completes. This happens inside a `pcall` wrapper, so cleanup runs even if your script errors.
</Info>

<Warning>
  **Do not** try to access macros from a different script or after your script has finished. They will always be `nil`. This is by design for security.
</Warning>

## Important notes

* Macros are **read-only** — modifying them has no effect on the server
* Macros are **per-execution** — values are fresh on every run
* If a user runs in **FFA mode** (no key), `L7_LinkedDiscordID` may be empty and `L7_UserNote` will be `"FFA"`
* `L7_SecondsLeft` returns the string `"Lifetime"` for keys without an expiry date, and a number (seconds) for timed keys
