ai-fuel for developers
How to read and set fuel with ai-fuel running, its state bags and its internal events.
ai-fuel has no exports. The fuel of a vehicle is the fuel entity state bag (0-100), kept by the server.
Read and set fuel with the lib's fuel.get(vehicle) and fuel.set(vehicle, level) (server; fuel.get also on the client). They go through ai-core's fuel layer (in Lua, call its exports GetVehicleFuel and SetVehicleFuel on ai-core), which reads and writes the fuel bag while ai-fuel runs and talks to the other fuel script when the server runs one instead. Your script then works with any fuel script.
Reading and setting fuel
import { fuel } from "@aicore-fivem/lib/server";
// Fill a vehicle the player just paid for at a custom station.
export function fillUp(vehicle: number): void {
const before = fuel.get(vehicle);
fuel.set(vehicle, 100);
console.log(`fuel ${before} -> 100`);
}Do not write the fuel bag from a client: the server refuses client writes (strict state bag mode), and ai-fuel takes only lower values from the driver's client anyway.
State bags
| Bag | On | Type | Meaning |
|---|---|---|---|
fuel | vehicle entity | number | Fuel 0-100. Set by the server. ai-vehicles seeds it when it spawns an owned vehicle. |
aiFuelNozzle | player | { x, y, z, netId } | undefined | The pump whose nozzle the player holds (x, y, z), and the network id of the vehicle it is plugged into (netId, null while in the hand). Every client near uses it to draw the nozzle and hose. |
local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
local level = Entity(vehicle).state.fuel -- number 0-100, or nil before the first setEvents
These are ai-fuel's own client-to-server events. They are listed so you can recognize them in logs; do not call them from other scripts.
| Name | Kind | What it does |
|---|---|---|
ai-fuel:consume | client event | (netId, fuel): the driver reports a lower level. Rated 2 per second; only lower values are taken. |
ai-fuel:takeNozzle | client callback | (x, y, z): take a pump's nozzle. Rated 1 per second. |
ai-fuel:plug | client callback | (netId | null): plug the nozzle into a vehicle, or take it out. Rated 2 per second. |
ai-fuel:returnNozzle | client event | Hang the nozzle back up. Rated 1 per second. |
ai-fuel:refuel | client callback | Runs the progress bar, charges and fills the tank. |