# Garage - Car key

## Add Car Key

This function, located in `gksphone/client/settings.lua`, is used to give the player the vehicle key. It works based on the vehicle key script installed on the server.

```lua
--- Function to give a key to a car
--- Located in: gksphone/client/settings.lua
--- Modify this function according to your key system
--- @param callback_vehicle number The vehicle entity
function GiveKeyCar(callback_vehicle)
  if GetResourceState('qs-vehiclekeys') == 'started' then
    local model = GetDisplayNameFromVehicleModel(GetEntityModel(callback_vehicle))
    local plate = GetVehicleNumberPlateText(callback_vehicle)
    exports['qs-vehiclekeys']:GiveKeys(plate, model, true)
  elseif GetResourceState('qb-vehiclekeys') == 'started' then
    local plate = GetVehicleNumberPlateText(callback_vehicle)
    TriggerEvent("vehiclekeys:client:SetOwner", plate)
  end
  -- New systems can be added here ↓↓↓
  -- local plate = GetVehicleNumberPlateText(callback_vehicle)
  -- Add the export or trigger of the key command you are using here
end
```

## Garage

You will edit the framework file you are using in the `gksphone/server/framework/` directory.

### Garage SQL

Set the default options for the following sections according to your garage script.

```lua
Config.GarageDBColumn = "garage" -- Set column name of garage name in owned_vehicles or player_vehicles database (esx: parking / qb: garage)
Config.GarageDefaultName = "pillboxgarage" -- Set the default garage name (default: pillboxgarage)
Config.GarageStored = "state = 1"  -- Vehicle garage status
```

### Car status info

This section determines whether the vehicle is outside or impounded according to the garage script.

```lua
function GetCharacterAllVehicles(identifier, appname)
    -- There are examples for the following other scripts
    -- You need to configure this section according to the garage script you are using.
    -- These values are found in the player_vehicles / owned_vehicles table.
    if GetResourceState("qb-garages") == "started"  then
        if vehicle.state == 2 then
            vehData.garage = "Impounded"
        elseif vehicle.state == 0 then
            vehData.garage = "Out"
        end
    elseif GetResourceState("cd_garage") == "started" or GetResourceState("jg-advancedgarages") == "started" then
        vehData.garage = vehicle.garage_id
        if not vehicle.in_garage then
            vehData.garage = "On The Street"
        end
        if vehicle.impound ~= 0 then
            vehData.garage = "Impounded"
        end
    elseif GetResourceState("loaf_garage") == "started" then
        if vehicle.state == 0 then
            vehData.garage = "On The Street"
        elseif vehicle.state == 2 then
            vehData.garage = "Impounded"
        end
    end
end
```

### Bring control

This function checks whether the car called is impounded or outside.

```lua
function GetVehicle(identifier, plate)
    -- The following sections are examples for other scripts.
    -- You should organize it according to your own script.
    -- You can view the values here in SQL.
    if GetResourceState("cd_garage") == "started" or GetResourceState("jg-advancedgarages") == "started" then
        if ret.impound ~= 0 then
            Debugprint("gksphone:server:vale:vehiclebring | Vehicle is impounded | CitizenID: " .. identifier .. " | Plate: " .. plate)
            return "carimpounded"
        elseif not ret.in_garage then
            Debugprint("gksphone:server:vale:vehiclebring | Vehicle is not in garage | CitizenID: " .. identifier .. " | Plate: " .. plate)
            return "carnotingarage"
        end
    elseif GetResourceState("loaf_garage") == "started" then
        if ret.stored == 2 then
            Debugprint("gksphone:server:vale:vehiclebring | Vehicle is impounded | CitizenID: " .. identifier .. " | Plate: " .. plate)
            return "carimpounded"
        elseif ret.stored == 0 then
            Debugprint("gksphone:server:vale:vehiclebring | Vehicle is not in garage | CitizenID: " .. identifier .. " | Plate: " .. plate)
            return "carnotingarage"
        end
    elseif GetResourceState("qb-garages") == "started" then
        if ret.state == 2 then
            Debugprint("gksphone:server:vale:vehiclebring | Vehicle is impounded | CitizenID: " .. identifier .. " | Plate: " .. plate)
            return "carimpounded"
        elseif ret.state == 0 then
            Debugprint("gksphone:server:vale:vehiclebring | Vehicle is not in garage | CitizenID: " .. identifier .. " | Plate: " .. plate)
            return "carnotingarage"
        end
    end    
end
```

### After Bring

Changing some parts in SQL when bringing a car

```lua
function VehicleUpdate(plate, app, data)
    -- Below are some examples of scripts.
    -- Setting to indicate that the car is outside via SQL after calling the car
    if GetResourceState("loaf_garage") == "started" then
        MySQL.Async.execute('UPDATE player_vehicles SET `state` = @state WHERE `plate` = @plate', {
            ['@plate'] = plate,
            ['@state'] = 0,
        })
    elseif GetResourceState("cd_garage") == "started" or GetResourceState("jg-advancedgarages") == "started" then
        MySQL.Async.execute('UPDATE player_vehicles SET  `in_garage` = @in_garage WHERE `plate` = @plate', {
            ['@plate'] = plate,
            ['@in_garage'] = 0,
        })
    elseif GetResourceState("qb-garages") == "started" then
        MySQL.Async.execute('UPDATE player_vehicles SET  `state` = @state WHERE `plate` = @plate', {
            ['@plate'] = plate,
            ['@state'] = 0,
        })
    end
end
```


---

# Agent Instructions: 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:

```
GET https://docs.gkshop.org/gksphone-v2/configuration/garage-car-key.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
