> For the complete documentation index, see [llms.txt](https://lscripts.gitbook.io/lscripts/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lscripts.gitbook.io/lscripts/scripts/l-revivestation/ambulance.md).

# Ambulance

l-revivestation ships set up for esx\_ambulancejob, but it is not tied to it. The complete death and revive handling sits in one file, `shared/ambulance.lua`. Nothing else in the resource knows which ambulance script you run, so that file is the only one you touch.

### The default file

```lua
Ambulance = {}

if IsDuplicityVersion() then
    function Ambulance.IsDead(src)
        return Player(src).state.isDead == true
    end

    function Ambulance.OnDeadChange(cb)
        AddStateBagChangeHandler('isDead', nil, function(bagName, _, value)
            local serverId = tonumber(bagName:match('^player:(%d+)$'))
            if serverId then
                cb(serverId, value == true)
            end
        end)
    end

    function Ambulance.Revive(targetId)
        TriggerClientEvent('esx_ambulancejob:revive', targetId)
    end
else
    function Ambulance.IsPlayerDead(serverId)
        return Player(serverId).state.isDead == true
    end
end
```

`IsDuplicityVersion()` is true on the server, so the first block is the server side and the `else` block the client side. Keep that split, only replace the bodies.

### What each function has to do

| Function                           | Side   | Task                                            |
| ---------------------------------- | ------ | ----------------------------------------------- |
| `Ambulance.IsDead(src)`            | Server | Returns whether that player is down             |
| `Ambulance.Revive(targetId)`       | Server | Revives that player                             |
| `Ambulance.IsPlayerDead(serverId)` | Client | Returns whether another player is down          |
| `Ambulance.OnDeadChange(cb)`       | Server | Optional, reports every change of a death state |

**`Ambulance.IsDead(src)`** runs for every entry of the list and once more right before the revive is paid out. Keep it a simple lookup, a table or state bag read. Never a database query.

**`Ambulance.Revive(targetId)`** is the complete revive. The station does not heal or respawn anyone on its own, it only makes this call, so point it at whatever your script offers, an event or an export.

**`Ambulance.IsPlayerDead(serverId)`** has to work on the client. The station looks for injured players around itself on the client side, which is what keeps the server free of per player loops. A replicated state bag like the default one is the easiest way to make a death state readable there.

{% hint style="info" %}
Your own death is not part of this file. l-revivestation reads that from ESX itself, which works with any ambulance script.
{% endhint %}

**`Ambulance.OnDeadChange(cb)`** is optional. If your script has a signal for "a player went down or was revived", hook it up and call `cb(serverId, isDead)`. Open station panels then update the moment it happens, so a player revived by a medic disappears from every list right away. If your script has nothing to hook into, delete the function. The list still keeps itself up to date, it just takes up to a second longer. No other feature depends on it.

### Example with exports

A script that exposes exports and its own event instead of a state bag:

```lua
function Ambulance.IsDead(src)
    return exports['my_ambulance']:IsPlayerDead(src)
end

function Ambulance.Revive(targetId)
    exports['my_ambulance']:RevivePlayer(targetId)
end

function Ambulance.OnDeadChange(cb)
    AddEventHandler('my_ambulance:deathStateChanged', function(serverId, isDead)
        cb(serverId, isDead)
    end)
end
```

And on the client, if that script replicates the state to everyone:

```lua
function Ambulance.IsPlayerDead(serverId)
    return exports['my_ambulance']:IsPlayerDead(serverId)
end
```

{% hint style="warning" %}
Restart the resource after editing the file and test with two players: one goes down near a station, the other opens the station and revives them. If the list stays empty, your `Ambulance.IsPlayerDead` does not return anything usable on the client.
{% endhint %}

### The other adapter files

Not ambulance related, but open for the same reason:

| File                   | What it holds                               |
| ---------------------- | ------------------------------------------- |
| `shared/functions.lua` | Notifications and the text prompt           |
| `client/framework.lua` | The ESX calls on the client                 |
| `server/framework.lua` | The ESX calls on the server, money and jobs |
