AI Core

Time and weather

Read and set the weather and game time, blackout and frozen time, through whichever weather script the server runs.

Time and weather go through ai-core, never through a weather script's own exports or events. ai-core forwards each call to the first weather script running: Renewed-Weathersync, av_weather, cd_easytime, then qb-weathersync (ai-weathersync answers under that name). They all follow the qb-weathersync contract. While none runs, every call answers null or false.

To pause the sync on one player's game (an interior with its own light, a cutscene), use the client side (PauseWorldSync, lib world.pauseSync); see Client exports.

GetWeather

The current weather type.

Side: Server · Waits: no · Lib: world.getWeather

Signature
GetWeather(): string | null
Lua
local weather = exports['ai-core']:GetWeather() -- 'EXTRASUNNY', 'RAIN'... or nil

Parameters

None.

Returns

string | null: the weather type in capitals (EXTRASUNNY, CLEAR, RAIN, THUNDER...). null when no weather script runs.

Example

import { resourceExport } from "@aicore-fivem/lib/server";

const core = resourceExport("ai-core");

/** Street races pay more in the rain. */
export function raceBonus(): number {
  const weather = core.GetWeather();
  return weather === "RAIN" || weather === "THUNDER" ? 1.25 : 1;
}

SetWeather

Sets the weather for everyone.

Side: Server · Waits: no · Lib: world.setWeather (takes the typed Weather names)

Signature
SetWeather(type: string): boolean
Lua
local ok = exports['ai-core']:SetWeather('RAIN')

Parameters

Prop

Type

Returns

boolean: true when the weather script accepted it. false when type is not a text, the script refused it (an unknown type) or no weather script runs.

Example

import { addCommand, resourceExport } from "@aicore-fivem/lib/server";
import { isWeather } from "@aicore-fivem/shared";

const core = resourceExport("ai-core");

addCommand("weather", { help: "Sets the weather", params: [{ name: "type", type: "string" }], restricted: true }, (_source, args) => {
  const type = args.type.toUpperCase();
  if (!isWeather(type)) return console.log("unknown weather type");
  core.SetWeather(type);
});

GetTime

The game time (the in-game clock).

Side: Server · Waits: no · Lib: world.getTime

Signature
GetTime(): { hour: number; minute: number } | null
Lua
local time = exports['ai-core']:GetTime() -- { hour, minute } or nil

Parameters

None.

Returns

{ hour, minute } | null: hour 0-23 and minute 0-59. null when no weather script runs.

Example

import { onClientCallback, resourceExport } from "@aicore-fivem/lib/server";
import { fail } from "@aicore-fivem/lib/shared";

const core = resourceExport("ai-core");

// The drug dealer only sells at night (22:00 - 05:59 game time).
onClientCallback("myscript:dealer", (source) => {
  const time = core.GetTime();
  const night = time !== null && (time.hour >= 22 || time.hour < 6);
  return night ? { ok: true } : fail("COME_BACK_AT_NIGHT");
});

SetTime

Sets the game time for everyone.

Side: Server · Waits: no · Lib: world.setTime

Signature
SetTime(hour: number, minute?: number): boolean
Lua
local ok = exports['ai-core']:SetTime(12, 0)

Parameters

Prop

Type

Returns

boolean: true when the weather script accepted it. false when hour is not a number, the script refused or no weather script runs.

Example

import { addCommand, resourceExport } from "@aicore-fivem/lib/server";

const core = resourceExport("ai-core");

addCommand("noon", { help: "Sets the time to 12:00", restricted: true }, () => {
  core.SetTime(12, 0);
});

SetBlackout

Turns the city's blackout (all artificial lights off) on or off.

Side: Server · Waits: no · Lib: world.setBlackout

Signature
SetBlackout(on: boolean): boolean
Lua
local ok = exports['ai-core']:SetBlackout(true)

Parameters

Prop

Type

Returns

boolean: true when the weather script accepted it. false when the script has no blackout, refused, or no weather script runs.

Example

import { resourceExport } from "@aicore-fivem/lib/server";

const core = resourceExport("ai-core");

/** The power plant was sabotaged: 5 minutes of darkness. */
export function sabotagePowerPlant(): void {
  if (!core.SetBlackout(true)) return;
  setTimeout(() => core.SetBlackout(false), 5 * 60_000);
}

FreezeTime

Stops or restarts the game clock.

Side: Server · Waits: no · Lib: world.freezeTime

Signature
FreezeTime(on: boolean): boolean
Lua
local ok = exports['ai-core']:FreezeTime(true)

Parameters

Prop

Type

Returns

boolean: true when the weather script accepted it. false when the script cannot freeze time, refused, or no weather script runs.

Example

import { addCommand, resourceExport } from "@aicore-fivem/lib/server";

const core = resourceExport("ai-core");

// An event night: always 21:00.
addCommand("eventnight", { help: "Freezes the clock at 21:00", restricted: true }, () => {
  core.SetTime(21, 0);
  core.FreezeTime(true);
});

On this page