# Interior Stuck Prevention

***

If a player leaves the server in the interiors of the underground garage, he will get stuck in it which will cause him to have to contact the administration to teleport him outside, with this option you can avoid such problems by writing the player coordinates when he leaves the server while in the interiors.

{% tabs %}
{% tab title="ESX" %}
{% hint style="warning" %}
**The code below is from ESX 1.10.7!**\
\
The function code may vary depending on your version of es\_extended.\
If the code is different, do not copy the code below, make your own changes to the lines that was marked `-- MODIFIED`, to avoid problems.
{% endhint %}

1. Navigate to es\_extended/server/functions.lua
2. Find function `Core.SavePlayer`
3. Replace the function with the one below or make the changes highlighted on the code below

```lua
function Core.SavePlayer(xPlayer, cb)
    if not xPlayer.spawned then
        return cb and cb()
    end

    local garageInterior, garageCoords = exports['vms_garagesv2']:isInInterior(xPlayer.source); -- MODIFIED
    updateHealthAndArmorInMetadata(xPlayer)
    local parameters <const> = {
        json.encode(xPlayer.getAccounts(true)),
        xPlayer.job.name,
        xPlayer.job.grade,
        xPlayer.group,
        json.encode(garageInterior and garageCoords or xPlayer.getCoords(false, true)), -- MODIFIED
        json.encode(xPlayer.getInventory(true)),
        json.encode(xPlayer.getLoadout(true)),
        json.encode(xPlayer.getMeta()),
        xPlayer.identifier,
    }

    MySQL.prepare(
        "UPDATE `users` SET `accounts` = ?, `job` = ?, `job_grade` = ?, `group` = ?, `position` = ?, `inventory` = ?, `loadout` = ?, `metadata` = ? WHERE `identifier` = ?",
        parameters,
        function(affectedRows)
            if affectedRows == 1 then
                print(('[^2INFO^7] Saved player ^5"%s^7"'):format(xPlayer.name))
                TriggerEvent("esx:playerSaved", xPlayer.playerId, xPlayer)
            end
            if cb then
                cb()
            end
        end
    )
end
```

4. Find function `Core.SavePlayers`
5. Replace the function with the one below or make the changes highlighted on the code below

```lua
function Core.SavePlayers(cb)
    local xPlayers <const> = ESX.Players
    if not next(xPlayers) then
        return
    end

    local startTime <const> = os.time()
    local parameters = {}

    for _, xPlayer in pairs(ESX.Players) do
        updateHealthAndArmorInMetadata(xPlayer)
        local garageInterior, garageCoords = exports['vms_garagesv2']:isInInterior(xPlayer.source); -- MODIFIED
        parameters[#parameters + 1] = {
            json.encode(xPlayer.getAccounts(true)),
            xPlayer.job.name,
            xPlayer.job.grade,
            xPlayer.group,
            json.encode(garageInterior and garageCoords or xPlayer.getCoords()),
            json.encode(xPlayer.getInventory(true)),
            json.encode(xPlayer.getLoadout(true)),
            json.encode(xPlayer.getMeta()),
            xPlayer.identifier,
        }
    end

    MySQL.prepare(
        "UPDATE `users` SET `accounts` = ?, `job` = ?, `job_grade` = ?, `group` = ?, `position` = ?, `inventory` = ?, `loadout` = ?, `metadata` = ? WHERE `identifier` = ?",
        parameters,
        function(results)
            if not results then
                return
            end

            if type(cb) == "function" then
                return cb()
            end

            print(("[^2INFO^7] Saved ^5%s^7 %s over ^5%s^7 ms"):format(#parameters, #parameters > 1 and "players" or "player", ESX.Math.Round((os.time() - startTime) / 1000000, 2)))
        end
    )
end
```

{% endtab %}

{% tab title="QB-Core" %}
{% hint style="warning" %}
**The code below is from QB-Core 1.2.6!**\
\
The function code may vary depending on your version of qb-core.\
If the code is different, do not copy the code below, make your own changes to the lines that was marked `-- MODIFIED`, to avoid problems.
{% endhint %}

1. Navigate to qb-core/server/player.lua
2. Find function `QBCore.Player.Save`
3. Replace the function with the one below or make the changes highlighted on the code below

```lua
function QBCore.Player.Save(source)
    local ped = GetPlayerPed(source)
    local garageInterior, garageCoords = exports['vms_garagesv2']:isInInterior(source); -- MODIFIED
    local pcoords = garageInterior and garageCoords or GetEntityCoords(ped) -- MODIFIED
    local PlayerData = QBCore.Players[source].PlayerData
    if PlayerData then
        MySQL.insert('INSERT INTO players (citizenid, cid, license, name, money, charinfo, job, gang, position, metadata) VALUES (:citizenid, :cid, :license, :name, :money, :charinfo, :job, :gang, :position, :metadata) ON DUPLICATE KEY UPDATE cid = :cid, name = :name, money = :money, charinfo = :charinfo, job = :job, gang = :gang, position = :position, metadata = :metadata', {
            citizenid = PlayerData.citizenid,
            cid = tonumber(PlayerData.cid),
            license = PlayerData.license,
            name = PlayerData.name,
            money = json.encode(PlayerData.money),
            charinfo = json.encode(PlayerData.charinfo),
            job = json.encode(PlayerData.job),
            gang = json.encode(PlayerData.gang),
            position = json.encode(pcoords),
            metadata = json.encode(PlayerData.metadata)
        })
        if GetResourceState('qb-inventory') ~= 'missing' then exports['qb-inventory']:SaveInventory(source) end
        QBCore.ShowSuccess(resourceName, PlayerData.name .. ' PLAYER SAVED!')
    else
        QBCore.ShowError(resourceName, 'ERROR QBCORE.PLAYER.SAVE - PLAYERDATA IS EMPTY!')
    end
end
```

{% endtab %}
{% endtabs %}
