Sleepless

RoutingExports

Server Exports

Last updated on

All of these exports are server-side. Call them with exports.sleepless_routing.

Bucket 0 is the global world. Bucket 1 is the hidden bucket. Named buckets start at 2.


getPlayerBucket

Returns the replicated state and the live routing bucket for a player.

local state = exports.sleepless_routing:getPlayerBucket(playerId)

Parameters

ParameterTypeDescription
playerIdnumberServer id of the player

Returns

TypeDescription
PlayerBucketState | nilnil when the player is not online

PlayerBucketState

FieldTypeDescription
currentBucketnumber | nilReplicated currentBucket state
oldBucketnumber | nilReplicated oldBucket state
bucketnumberLive value from GetPlayerRoutingBucket

currentBucket is the last bucket this resource published. bucket is the routing bucket the game is using right now.

Example

local state = exports.sleepless_routing:getPlayerBucket(source)
if not state then return end

print(state.currentBucket, state.oldBucket, state.bucket)

addPlayerToBucket

Moves a player into a bucket. The vehicle they are sitting in moves with them, including the other players in that vehicle and any non-player peds in its seats. Objects attached to the ped move too.

Replicates oldBucket, currentBucket, and currentBucketName on that player. currentBucketName is the registered name, or nil for buckets 0 and 1.

local ok = exports.sleepless_routing:addPlayerToBucket(playerId, bucket, force)

Parameters

ParameterTypeDescription
playerIdnumberServer id of the player
bucketnumberTarget routing bucket. 0 is valid
forceboolean?When true, apply the move even if the player is already in that bucket. Defaults to false

Returns

TypeDescription
booleantrue when the player is in the requested bucket afterward

Pass true for force when the caller needs attached entities moved again, or when the replicated state may be stale.

Example

local bucketId = exports.sleepless_routing:requestBucketId('apartment_4b', false)
if not bucketId then return end

exports.sleepless_routing:addPlayerToBucket(source, bucketId, true)

routePlayerToHiddenBucket

Moves a player into bucket 1. Population in that bucket is disabled for the life of the resource.

local ok = exports.sleepless_routing:routePlayerToHiddenBucket(playerId)

Parameters

ParameterTypeDescription
playerIdnumberServer id of the player

Returns

TypeDescription
booleantrue when the player is in the hidden bucket

Example

exports.sleepless_routing:routePlayerToHiddenBucket(source)

routePlayerToGlobalBucket

Moves a player into bucket 0, the shared world.

local ok = exports.sleepless_routing:routePlayerToGlobalBucket(playerId)

Parameters

ParameterTypeDescription
playerIdnumberServer id of the player

Returns

TypeDescription
booleantrue when the player is in the global bucket

Example

RegisterNetEvent('myresource:leftInterior', function()
    exports.sleepless_routing:routePlayerToGlobalBucket(source)
end)

createBucketId

Registers a new named bucket. If the name already exists, the existing id is returned and the population and lockdown arguments are ignored.

Population and lockdown apply only on the first create. An omitted population value defaults to false. An omitted lockdown leaves FiveM's default, inactive.

local bucketId = exports.sleepless_routing:createBucketId(name, population, lockdown)

Parameters

ParameterTypeDescription
namestringNon-empty bucket name
populationboolean?SetRoutingBucketPopulationEnabled for the new bucket. Defaults to false
lockdown'inactive' | 'relaxed' | 'strict' | 'no_dummy' | 'full'?SetRoutingBucketEntityLockdownMode. An unknown string makes the call return nil

strict blocks client-created entities. Leave lockdown unset when a script inside the instance spawns networked entities from the client.

Returns

TypeDescription
number | nilBucket id, or nil when name is empty or lockdown is not a known mode

Example

local quiet = exports.sleepless_routing:createBucketId('intro', false, 'strict')
local city = exports.sleepless_routing:createBucketId('public_event', true)

requestBucketId

Returns the id for a name. Creates the bucket when the name is not registered yet.

local bucketId = exports.sleepless_routing:requestBucketId(name, population, lockdown)

Parameters

ParameterTypeDescription
namestringNon-empty bucket name
populationboolean?Used only when this call creates the bucket. Defaults to false
lockdown'inactive' | 'relaxed' | 'strict' | 'no_dummy' | 'full'?Used only when this call creates the bucket. An unknown string makes the call return nil

Returns

TypeDescription
number | nilBucket id, or nil when name is empty or lockdown is not a known mode

Example

local bucketId = exports.sleepless_routing:requestBucketId(('house_%s'):format(propertyId), false)
exports.sleepless_routing:addPlayerToBucket(source, bucketId, true)

getBucketId

Looks up a name and returns its id when that name is already registered.

local bucketId = exports.sleepless_routing:getBucketId(name)

Parameters

ParameterTypeDescription
namestringBucket name

Returns

TypeDescription
number | nilBucket id, or nil when the name is not registered

Example

local bucketId = exports.sleepless_routing:getBucketId('house_12')
if not bucketId then return end

getBucketName

Looks up the name for a bucket id. Buckets 0 and 1 are unnamed.

local name = exports.sleepless_routing:getBucketName(bucketId)

Parameters

ParameterTypeDescription
bucketIdnumberRouting bucket id

Returns

TypeDescription
string | nilRegistered name, or nil

Example

local state = exports.sleepless_routing:getPlayerBucket(source)
local name = state and exports.sleepless_routing:getBucketName(state.bucket)

removeBucketId

Forgets a registered name. The numeric bucket is not deleted, and players still inside it stay there. Their currentBucketName state is cleared.

A later requestBucketId with the same name allocates a new id.

local removed = exports.sleepless_routing:removeBucketId(name)

Parameters

ParameterTypeDescription
namestringBucket name

Returns

TypeDescription
booleantrue when the name was registered

Example

exports.sleepless_routing:routePlayerToGlobalBucket(source)
exports.sleepless_routing:removeBucketId(('house_%s'):format(propertyId))

setBucketOptions

Changes population, lockdown, or both on a bucket that already exists. Pass a bucket id or a registered name. Bucket 0 and bucket 1 accept a numeric id.

local ok = exports.sleepless_routing:setBucketOptions(bucket, options)

Parameters

ParameterTypeDescription
bucketnumber | stringBucket id, or a name from requestBucketId
options.populationboolean?SetRoutingBucketPopulationEnabled
options.lockdown'inactive' | 'relaxed' | 'strict' | 'no_dummy' | 'full'?SetRoutingBucketEntityLockdownMode

Returns

TypeDescription
booleantrue when the bucket resolved and every supplied option was applied

Example

exports.sleepless_routing:setBucketOptions('house_12', {
    lockdown = 'strict',
})

getPlayersInBucket

Returns the server ids of players whose live routing bucket matches. This reads GetPlayerRoutingBucket, so it includes players moved by another resource.

local playerIds = exports.sleepless_routing:getPlayersInBucket(bucket)

Parameters

ParameterTypeDescription
bucketnumber | stringBucket id, or a name from requestBucketId

Returns

TypeDescription
number[] | nilServer ids. {} when the bucket is empty. nil when the name is not registered or the id is invalid

Example

local playerIds = exports.sleepless_routing:getPlayersInBucket(('house_%s'):format(propertyId)) or {}

for i = 1, #playerIds do
    exports.sleepless_routing:routePlayerToGlobalBucket(playerIds[i])
end

triggerClientEventForBucket

Sends one client event to every player currently in the bucket. This wraps lib.triggerClientEvent, so the payload is packed once and delivered to each player in that bucket.

The argument order matches lib.triggerClientEvent: event name, then the target, then the payload. The target is a bucket id or a registered name instead of a server id.

local sent = exports.sleepless_routing:triggerClientEventForBucket(eventName, bucket, ...)

Parameters

ParameterTypeDescription
eventNamestringClient event name
bucketnumber | stringBucket id, or a name from requestBucketId
...anyArguments forwarded to the client event

Returns

TypeDescription
booleantrue when the bucket resolved and the event was handed to lib.triggerClientEvent. An empty bucket still returns true. false when the event name is empty, the name is not registered, or the id is invalid

Example

exports.sleepless_routing:triggerClientEventForBucket('myresource:client:houseUpdated', ('house_%s'):format(propertyId), furniture)

moveEntityToBucket

Moves one entity into a bucket. Pass a bucket id, or the name used with requestBucketId. Use addPlayerToBucket for players.

local ok = exports.sleepless_routing:moveEntityToBucket(entityId, bucket)

Parameters

ParameterTypeDescription
entityIdnumberEntity handle
bucketnumber | stringBucket id, or a registered name

Returns

TypeDescription
booleantrue when the entity exists, the bucket resolved, and the entity ends in that bucket

Example

local bucketId = exports.sleepless_routing:requestBucketId(('garage_%s'):format(propertyId), false)
exports.sleepless_routing:moveEntityToBucket(vehicle, bucketId)

moveEntityToGlobalBucket

Moves one entity into bucket 0. Use addPlayerToBucket for players.

local ok = exports.sleepless_routing:moveEntityToGlobalBucket(entityId)

Parameters

ParameterTypeDescription
entityIdnumberEntity handle

Returns

TypeDescription
booleantrue when the entity exists and ends in bucket 0

Example

exports.sleepless_routing:moveEntityToGlobalBucket(vehicle)

On this page