TK-Lib is the core library for all TK Studios resources.
TK Weed has 2 dependencies:
- oxmysql (Comes with QBCore)
- QBCore
Ensure tk-lib/Server/Config/index.js
has the correct inventory configuration option set.
Ensure that tk-lib
is in your resource folder. We reccommend putting all TK Studios resources in a folder called [tk-studios]
.
Add ensure [tk-studios]
to your server.cfg
Reboot your server
The following exports are for use in server side scripts. For client side exports please scroll down.
const Lib = global.exports["tk-lib"].GetLib();
/**
* Executes a database query.
*
* @async
* @param {string} Query - The SQL query to execute.
* @param {Array} [Params] - The parameters to be used in the query.
* @returns {Promise<Array>} - The result of the query.
*/
await Lib.DB.Run("DELETE FROM ? WHERE id = ?", ["db_name", 1]);
/**
* Executes a database query, returning 1 row or null.
*
* @async
* @param {string} Query - The SQL query to execute.
* @param {Array} [Params] - The parameters to be used in the query.
* @returns {Promise<Object>} - The result of the query.
*/
await Lib.DB.Get("SELECT * FROM ? WHERE id = ?", ["db_name", 1]);
/**
* Executes a database query, returning all matching rows.
*
* @async
* @param {string} Query - The SQL query to execute.
* @param {Array} [Params] - The parameters to be used in the query.
* @returns {Promise<Array[Object]>} - The result of the query.
*/
await Lib.DB.All("SELECT * FROM ?", ["db_name"]);
/**
* Creates a new Logger instance.
*
* @param {string} resourceName - The name of the resource.
* @param {string} fileName - The name of the file.
*/
const Logger = Lib.Functions.Logger(GetCurrentResourceName(), "Main");
Logger.log("Hello World with TK-Lib");
Logger.warn("Hello World with TK-Lib");
Logger.error("Hello World with TK-Lib");
Logger.success("Hello World with TK-Lib");
Logger.alert("Hello World with TK-Lib");
Debug logs are only visible if
Config.Debug
is set totrue
intk-lib/server/config/index.js
Logger.debug("Hello World with TK-Lib");
Trace logs trace the call stack showing where the log was triggered from.
Logger.trace("Hello World with TK-Lib");
/**
* Waits for the specified amount of time.
*
* @async
* @param {number} ms - The number of milliseconds to wait.
* @returns {Promise<boolean>} - A promise that resolves to true after the specified time has elapsed.
*/
await Lib.Functions.Wait(500); // Time in MS
Lib.Functions.Notify(3, "success", "Test?", 3000); // Send an error notification to player 3 lasting for 3 seconds.
Lib.Functions.Notify(3, "error", "Do ya like jazz?", Lib.Time.Second * 5); // Send an error notification to player 3 lasting for 5 seconds.
let HasItem = Lib.Functions.HasItem(3, "weapon_combatpistol", 1); // Returns true if Server ID 3 has a combat pistol.
Lib.Functions.AddItem(3, "weapon_hatchet", 1); // Gives a hatchet to Server ID 3.
Lib.Functions.AddItem(3, "weapon_combatpistol", 1, 5, {}); // Gives 1 combat pistol to Server ID 3 in slot 5 with no metadata.
let ItemRemoved = Lib.Functions.RemoveItem(3, "weapon_hatchet", 1); // Removes 1 hatchet from Server ID 3.
Lib.Functions.GetMoney(3, "cash");
Lib.Functions.SetMoney(3, "cash", 100); // Set player's 'cash' to 100
Lib.Functions.AddMoney(3, "bank", 150); // Gives player $150 'bank' money
let Success = Lib.Functions.RemoveMoney(3, "bank", 150); // Removes $150 from player, returns false if player does not have enough money.
Lib.Functions.KickPlayer(3, "Attempted Abuse"); // Kick Server ID 3 for 'Attempted Abuse'
let PlayersList = Lib.Functions.GetPlayersArray(); // [ 3, 14, 24 ] Array of Server IDs.
/**
* Retrieves the identifiers associated with a player.
*
* @param {string} src - The source of the player.
* @returns {string[]} - An array of player identifiers.
*/
const Identifier = Lib.Functions.GetPlayerIdentifiers(3); // [ 'fivem:12345', 'discord:148764657107075072', ... ]
/**
* Retrieves the player identifier based on the given source and identifier type.
*
* @param {string} src - The source of the player.
* @param {string} identifier - The type of identifier to retrieve.
* @returns {string|null} The player identifier, or null if not found.
*/
const Identifier = Lib.Functions.GetPlayerIdentifier(3, "discord"); // 'discord:148764657107075072'
/**
* Retrieves the stripped player identifier based on the given identifier type.
*
* @param {string} src - The source of the player.
* @param {string} identifier - The identifier type.
* @returns {string|null} The stripped player identifier, or null if not found.
*/
const Identifier = Lib.Functions.GetStrippedPlayerIdentifier(3, "discord"); // '148764657107075072'
// Players
let src = 3;
let BucketID = Lib.Buckets.CreateRoutingBucket(false, "strict"); // Creates a new routing bucket with AI Population disabled & entity lockdown mode as strict. Options are ['strict', 'relaxed', 'inactive']
let { ID, PopulationEnabled, EntityLockdownMode } = Lib.Buckets.GetRoutingBucket(BucketID); // Fetch bucket object.
Lib.Buckets.SetPlayerBucket(src, BucketID);
Lib.Buckets.ClearPlayerRoutingBucket(src); // Return player to default routing bucket.
// Entities
let vehicle = 14281;
Lib.Buckets.SetEntityBucket(vehicle, BucketID);
const Second = Lib.Time.Second; // 1 Second in MS
const Minute = Lib.Time.Minute; // 1 Minute in MS
const Hour = Lib.Time.Hour; // 1 Hour in MS
const Day = Lib.Time.Day; // 1 Day in MS
const Week = Lib.Time.Week; // 1 Week in MS
const Month = Lib.Time.Month; // 1 Month in MS
const Year = Lib.Time.Year; // 1 Year in MS
const CurrentEpoch = Lib.Time.GetEpoch(); // Current Epoch Time in MS
const FullReadableTime = Lib.Time.EpochToReadableDateString(CurrentEpoch); // Readable Date String (Full)
const ShortReadableTime = Lib.Time.EpochToShortDateString(CurrentEpoch); // DD/MM/YYYY Readable Date String (Short)
const ReadableDateTimeString = Lib.Time.EpochToReadableDateTimeString(CurrentEpoch); // Full Date + Time
const ReadableTimeString = Lib.Time.MSToReadableTimeString(Hour * 3); // 3h 0m 0s
Lib.Maths.AverageArray([1, 2, 3]); // 2
Lib.Maths.MinArray([1, 2, 3]); // 1
Lib.Maths.MaxArray([1, 2, 3]); // 3
let Vec2 = Lib.Maths.Vector2(0, 0); // X, Y
let Vec4 = Lib.Maths.Vector3(4, 4, 4); // X, Y, Z
let Vec4 = Lib.Maths.Vector4(4, 4, 4, 270); // X, Y, Z, W
let Pos1 = Lib.Maths.Vector2(0, 0); // X, Y
let Pos2 = Lib.Maths.Vector2(4, 4); // X, Y
Lib.Maths.Distance2D(Pos1.x, Pos1.y, Pos2.x, Pos2.y); // Distance between 2 X,Y Coordinate Sets
let Pos1 = Lib.Maths.Vector2(0, 0); // X, Y
let Pos2 = Lib.Maths.Vector2(4, 4); // X, Y
Lib.Maths.DistanceVector2(Pos1, Pos2); // Distance between 2 vector2 coordinates.
let Pos1 = Lib.Maths.Vector3(0, 0, 0); // X, Y, Z
let Pos2 = Lib.Maths.Vector3(4, 4, 4); // X, Y, Z
Lib.Maths.Distance3D(Pos1.x, Pos1.y, Pos1.z, Pos2.x, Pos2.y, Pos2.z); // Distance between 2 X,Y,Z Coordinate Sets (X1, Y1, Z1, X2, Y2, Z2)
let Pos1 = Lib.Maths.Vector3(0, 0, 0); // X, Y, Z
let Pos2 = Lib.Maths.Vector3(4, 4, 4); // X, Y, Z
Lib.Maths.DistanceVector3(Pos1, Pos2); // Distance between 2 vector3 coordinates.
Accepted Currencies are: USD, EUR, GBP, AUD, CAD, JPY, CHF, CNY, SEK, NZD, NOK, MXN, SGD, HKD, KRW, TRY, INR, RUB, BRL, ZAR, PLN
Lib.Maths.FormatAsCurrency(5000, "USD"); // $5,000.00
The following exports are for use in client side scripts. For server side exports please scroll up.
const Lib = global.exports["tk-lib"].GetLib();
let Coords = await Lib.Functions.ObjectPlacementUI("object_name"); // Returns false if invalid coords or cancelled.
/**
* Creates a new Logger instance.
*
* @param {string} resourceName - The name of the resource.
* @param {string} fileName - The name of the file.
*/
const Logger = Lib.Functions.Logger(GetCurrentResourceName(), "Main");
Logger.log("Hello World with TK-Lib");
Logger.warn("Hello World with TK-Lib");
Logger.error("Hello World with TK-Lib");
Logger.success("Hello World with TK-Lib");
Logger.alert("Hello World with TK-Lib");
Debug logs are only visible if
Config.Debug
is set totrue
intk-lib/server/config/index.js
Logger.debug("Hello World with TK-Lib");
/**
* Waits for the specified amount of time.
*
* @async
* @param {number} ms - The number of milliseconds to wait.
* @returns {Promise<boolean>} - A promise that resolves to true after the specified time has elapsed.
*/
await Lib.Functions.Wait(500); // Time in MS