A player can own several characters, but only one is active at a time. jrs_core loads that character, marks it ready and copies the basics into player state bags. Server scripts use the exports.jrs_core:* calls below, client scripts listen for the ready event or read LocalPlayer.state. jrs_character (the selector and creator) adds a few extra exports for age, slots and the player model.
Never give items, money or blips before the character is ready. Wait for jrs_core:server:characterReady on the server or jrs_core:client:characterReady on the client, or check IsCharacterReady first.
Load And Unload Events#
The order on a normal join is: characterLoaded, then characterReady, then jrs:SelectedCharacter on the client. When the player goes back to the selector or leaves, characterUnloaded fires.
AddEventHandler('jrs_core:server:characterReady', function(source, character) ... end)Fires once the core has accepted the chosen character and the state bags are set. This is the safe place for server setup per character.
AddEventHandler('jrs_core:server:characterReady', function(source, character)
print(('%s %s is in town'):format(character.firstname, character.lastname))
end)| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
character | table | The live character object (same one GetCharacter returns). |
AddEventHandler('jrs_core:server:characterLoaded', function(source, character) ... end)Fires right before characterReady with the same arguments. Use it when your code must run ahead of the ready listeners.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
character | table | The live character object. |
AddEventHandler('jrs_core:server:characterUnloaded', function(source, character, reason) ... end)Fires when the active character is dropped: player left, opened the selector, or the core cleared it. State bags are already reset.
AddEventHandler('jrs_core:server:characterUnloaded', function(source, character, reason)
ActiveWagons[source] = nil
end)| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
character | table | The character that was active, or nil if none was. |
reason | string | Why it happened, for example dropped, joining, clear or unknown. |
RegisterNetEvent('jrs_core:client:characterReady', function(charId) ... end)Sent to the player's client when the character is ready. The same moment is also raised locally as jrs_core:characterReady, so you can use AddEventHandler without a net event.
AddEventHandler('jrs_core:characterReady', function(charId)
CreateMyBlips()
end)| Argument | Type | Meaning |
|---|---|---|
charId | number | Character id. |
RegisterNetEvent('jrs_core:client:characterLoaded', function(charId) ... end)Client copy of the loaded step, sent just before characterReady. Also raised locally as jrs_core:characterLoaded.
| Argument | Type | Meaning |
|---|---|---|
charId | number | Character id. |
RegisterNetEvent('jrs_core:client:characterUnloaded', function(reason) ... end)Tells the client its character is gone. Stop loops and hide your UI here. Also raised locally as jrs_core:characterUnloaded.
| Argument | Type | Meaning |
|---|---|---|
reason | string | Same reason text the server event gets. |
RegisterNetEvent('jrs:SelectedCharacter', function(charId) ... end)Older client event sent after selection, once the character is ready. Many JRS resources still listen to it, so it keeps firing. It also fires again on every character switch.
RegisterNetEvent('jrs:SelectedCharacter', function(charId)
TriggerServerEvent('jrs_myscript:server:loadData')
end)| Argument | Type | Meaning |
|---|---|---|
charId | number | Character id. |
Other Character Events#
AddEventHandler('jrs_core:server:characterCreated', function(source, charId, identifier) ... end)Fires when a brand new character row is written to the database.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
charId | number | The new character id. |
identifier | string | Account identifier that owns it. |
AddEventHandler('jrs_core:server:firstCharacterSetup', function(source, charId) ... end)Fires after all starter hooks have run for a new character. Use RegisterCharacterStarterHook if you want to hand out items, this event is for logging or follow-up work.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
charId | number | Character id. |
AddEventHandler('jrs_core:server:characterSaved', function(source, character) ... end)Fires after SaveCharacter writes the character to the database.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
character | table | The live character object. |
AddEventHandler('jrs_core:server:characterIdentityChanged', function(source, charId, old, new) ... end)Fires when the name, nickname, occupation or story of the active character changes. Resources that keep their own copy of the name (ID cards, records) should update it here.
AddEventHandler('jrs_core:server:characterIdentityChanged', function(source, charId, old, new)
MySQL.update('UPDATE my_records SET name = ? WHERE charid = ?', { new.firstname .. ' ' .. new.lastname, charId })
end)| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
charId | number | Character id. |
old | table | Fields before the change: firstname, lastname, nickname, occupation, lore. |
new | table | The same fields after the change. |
RegisterNetEvent('jrs_core:client:characterIdentityChanged', function(new) ... end)Client side of the identity change, sent only to the player whose character changed.
| Argument | Type | Meaning |
|---|---|---|
new | table | The new firstname, lastname, nickname, occupation and lore. |
AddEventHandler('jrs_core:server:appearanceChanged', function(source, charId, data) ... end)Fires when the saved look of an online character changes through SaveAppearance or RefreshAppearanceCache.
| Argument | Type | Meaning |
|---|---|---|
source | number | Server id of the player using that character. |
charId | number | Character id. |
data | table | The appearance that was saved or reloaded. |
RegisterNetEvent('jrs_core:client:appearanceChanged', function(charId, version) ... end)Sent to the player after RefreshAppearanceCache, so the client can redraw the ped.
| Argument | Type | Meaning |
|---|---|---|
charId | number | Character id. |
version | number | New appearance version number. |
AddEventHandler('jrs_core:server:metadataChanged', function(source, key, value) ... end)Fires each time SetMetadata, AddMetadata or RemoveMetadata stores a value.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
key | string | The metadata key. |
value | any | The new value. |
AddEventHandler('jrs_core:server:characterDeleteHook', function(charId, identifier, phase) ... end)Fires after the registered delete hooks have run for a character that is being deleted.
| Argument | Type | Meaning |
|---|---|---|
charId | number | Character id. |
identifier | string | Owner account identifier. |
phase | string | Delete phase, before when not given. |
Is The Character Ready#
exports.jrs_core:IsCharacterReady(source)Checks that the player has a fully loaded character. Call it at the top of any server event that touches character data.
RegisterNetEvent('jrs_myscript:server:buy', function()
local src = source
if not exports.jrs_core:IsCharacterReady(src) then return end
-- safe to continue
end)| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
exports.jrs_core:IsIdentityReady(source)Same check as IsCharacterReady, named for identity gates (the player has a verified character on their account).
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
exports.jrs_core:GetCharacter(source)Gives the live character object of an online player. Changes you make to it stay in memory until the next save.
local char = exports.jrs_core:GetCharacter(source)
if char then print(char.charIdentifier, char.job) end| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
exports.jrs_core:GetCharacterState(source)Small status table, handy for debugging or for passing to another resource.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
ready, version (jrs_core version), identifier and charIdentifier.exports.jrs_core:IsCharacterReady()Client check based on the CharacterReady state bag.
exports.jrs_core:WaitForCharacterReady(timeoutMs?)Blocks the current thread until the character is ready or the time runs out. Use it inside a CreateThread.
CreateThread(function()
if exports.jrs_core:WaitForCharacterReady(15000) then
SpawnCampfire()
end
end)| Argument | Type | Meaning |
|---|---|---|
timeoutMs | number | How long to wait. Default 10000. |
exports.jrs_core:GetCharacterStateLocal()Reads the local player's state bags into one table.
ready, charIdentifier, identifier, firstname, lastname, job, jobGrade, jobLabel, onDuty, gender.exports.jrs_character:IsCharacterReady()Stricter client check from the selector: true only when a character is loaded and the selector or creator screen is closed.
exports.jrs_character:IsCharacterGateActive()Tells you if the selector or creator is on screen. Hide your HUD while it is.
Find Characters#
exports.jrs_core:GetCharacterById(charId)Looks for the character among online players first and falls back to the database.
local char, src,
if online then
TriggerClientEvent('jrs_core:notify', src, 'You have mail')
end| Argument | Type | Meaning |
|---|---|---|
charId | number | Character id. |
exports.jrs_core:GetOfflineCharacter(charId)Reads the character row straight from the database, even if the player is online. JSON columns (multijobs, skills, meta) come back decoded.
| Argument | Type | Meaning |
|---|---|---|
charId | number | Character id. |
exports.jrs_core:GetCharactersByIdentifier(identifier)Lists every character of one account, oldest first. The list is capped by Config.CharacterService.OfflineLookupLimit (50 by default, 200 max).
| Argument | Type | Meaning |
|---|---|---|
identifier | string | Account identifier, for example a steam id. |
exports.jrs_core:FindCharactersByName(firstname, lastname?)Exact name search, most recently played first. Leave the last name out to match on first name only.
local found = exports.jrs_core:FindCharactersByName('Arthur', 'Morgan')
for _, row in ipairs(found) do print(row.charIdentifier) end| Argument | Type | Meaning |
|---|---|---|
firstname | string | First name. |
lastname | string | Last name. |
exports.jrs_core:GetPlayerObject(source)One table with the player's data and bound helper functions, so you do not need to pass the source again.
local p = exports.jrs_core:GetPlayerObject(source)
if p and p.Functions.GetMoney() >= 25 then
p.Functions.RemoveMoney(25)
end| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
source, PlayerData (a flat copy with money, job, skills, metadata and more), Character (the live object) and Functions (AddMoney, RemoveMoney, GetMoney, SetJob, SetDuty, ToggleDuty, GetMetadata, SetMetadata, AddSkillXP, Save).exports.jrs_character:GetCharacterSummaries(source)Safe overview of an online player's own characters, without money, inventory or coordinates. Soft deleted characters are left out. At most 12 rows.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
charId, firstname, lastname, nickname, occupation, gender, job, jobGrade, dateOfBirth, isDead, lastPlayed, active.Character Slots#
exports.jrs_core:GetCharacterCount(source)Counts the characters saved for the player's account.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
exports.jrs_core:GetMaxCharacters(source)How many characters this account may have. Uses the user's own limit, then Config.Char_Count, and never less than 1.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
exports.jrs_core:CanCreateCharacter(source)Shortcut for count below limit.
if not exports.jrs_core:CanCreateCharacter(source) then
TriggerClientEvent('jrs_core:notify', source, 'All slots are taken')
end| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
exports.jrs_core:SetMaxCharacters(source, count)Saves a new slot limit for the player's account (minimum 1). Good for VIP rewards.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
count | number | New limit. |
exports.jrs_character:GetCharacterSlots(source)Slot numbers as the selector sees them, including role based slots. Soft deleted characters do not count.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
limit, used, free, source (where the limit came from) and max (hard cap).exports.jrs_character:GetCharacterSlotsForIdentifier(identifier)Same as GetCharacterSlots for any account, online or not. Role based slots are not applied because they need a live player.
| Argument | Type | Meaning |
|---|---|---|
identifier | string | Account identifier. |
GetCharacterSlots.Identity And Age#
exports.jrs_core:SetCharacterIdentity(source, fields)Renames the active character everywhere at once: database, live object, core caches and state bags. It does not check the text for bad words, the caller must do that. Fires the identity changed events.
local ok, why = exports.jrs_core:SetCharacterIdentity(source, {
firstname = 'John', lastname = 'Marston', nickname = 'Johnny'
})| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
fields | table | firstname and lastname (both required), plus optional nickname, occupation and lore. |
character_not_ready, name_required, db_failed.exports.jrs_character:GetCharacterAge(source)Current in-game age of the active character, based on birth date and play time.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
exports.jrs_character:GetCharacterAgeById(charId)Same as GetCharacterAge for any character id, online or offline.
| Argument | Type | Meaning |
|---|---|---|
charId | number | Character id. |
exports.jrs_character:GetCurrentCharacterAge()Local copy of the age the client is using for the ped.
Appearance#
In GetAppearance, SaveAppearance and RefreshAppearanceCache a plain number is always a character id, never a server id. To pass a player, use the ForSource version or { source = src }.
exports.jrs_core:GetAppearance(charIdOrRef)Loads the saved look of any character, online or not. Clothes come from jrs_clothing and hair from jrs_barber when those are started.
local look = exports.jrs_core:GetAppearance(charId)| Argument | Type | Meaning |
|---|---|---|
charIdOrRef | number|table | Character id, or a table with charIdentifier or source. |
charIdentifier, skin, components, version, and when available outfitId, hiddenCategories, barber.exports.jrs_core:GetAppearanceForSource(source)Look of the character an online player is using.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
GetAppearance.exports.jrs_core:SaveAppearance(charIdOrRef, data)Writes a new look and bumps the appearance version. Only the parts you include are changed. Clothes and barber data are passed on to jrs_clothing and jrs_barber.
local look = exports.jrs_core:GetAppearance(charId)
look.skin.Eyes = 3
exports.jrs_core:SaveAppearance(charId, { skin = look.skin })| Argument | Type | Meaning |
|---|---|---|
charIdOrRef | number|table | Character id or reference table. |
data | table | Any of skin, components, outfitId, barber. |
invalid_data / character_not_found.exports.jrs_core:RefreshAppearanceCache(charIdOrRef)Reloads the look from the database into the live character and tells the client to redraw. Use it after you edit the tables yourself.
| Argument | Type | Meaning |
|---|---|---|
charIdOrRef | number|table | Character id or reference table. |
exports.jrs_core:RefreshAppearanceForSource(source)RefreshAppearanceCache for the character an online player is using.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
RefreshAppearanceCache.exports.jrs_character:ReloadCurrentCharacter()Rebuilds the local ped from the saved look. It does not unload the character or open the selector.
exports.jrs_character:IsReloadingCurrentCharacter()Tells you if ReloadCurrentCharacter is still busy.
exports.jrs_character:RestoreCharacterAppearance(ped?)Puts the saved look back on a ped, for example after a respawn or a model swap.
| Argument | Type | Meaning |
|---|---|---|
ped | number | Ped handle. Defaults to the player ped. |
Metadata#
Small values stored per character in the meta column. Good for flags and counters that do not need their own table.
exports.jrs_core:GetMetadata(source, key?, default?)Reads one value, or the whole metadata table when no key is given.
local done = exports.jrs_core:GetMetadata(source, 'tutorial_done', false)| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
key | string | The key to read. |
default | any | Returned when the key is missing or no character is loaded. |
exports.jrs_core:SetMetadata(source, key, value)Stores a value and saves it to the database right away.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
key | string | The key. |
value | any | Any value that fits in JSON. |
exports.jrs_core:AddMetadata(source, key, amount)Adds a number to a numeric key (missing keys start at 0).
exports.jrs_core:AddMetadata(source, 'wolves_hunted', 1)| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
key | string | The key. |
amount | number | How much to add. |
exports.jrs_core:RemoveMetadata(source, key, amount)Subtracts a number from a numeric key. It does not delete the key.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
key | string | The key. |
amount | number | How much to take off. |
Skills#
Per character skill XP. Level size and cap come from Config.CharacterService.Skills[name] (xpPerLevel and maxLevel, both 100 by default).
exports.jrs_core:GetSkills(source)All skills stored for the active character.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
exports.jrs_core:GetSkill(source, skillName)One skill with its level worked out.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
skillName | string | Skill name. |
xp, level, maxLevel, nextLevelXp, label.exports.jrs_core:SetSkillXP(source, skillName, xp)Sets the XP of a skill and saves it. When the level changes, jrs_core:server:skillLevelChanged and jrs_core:client:skillLevelChanged fire with the skill, new level and old level.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
skillName | string | Skill name. |
xp | number | New XP, at least 0. |
exports.jrs_core:AddSkillXP(source, skillName, amount)Adds XP on top of the current amount.
exports.jrs_core:AddSkillXP(source, 'fishing', 10)| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
skillName | string | Skill name. |
amount | number | XP to add. |
SetSkillXP.Jobs, multi job and duty calls (AddJob, SwitchJob, SetDuty and the rest) are on the JRS API page.
Saving And Health#
exports.jrs_core:SaveCharacter(source)Writes name, job, duty, multi jobs, skills and metadata of the live character to the database and updates the last played time.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
exports.jrs_core:SaveHealthState(source, healthState)Stores a health snapshot (any table you choose) for the active character.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
healthState | table | The data to keep. |
exports.jrs_core:GetHealthState(charIdOrRef)Reads the stored health snapshot of any character.
| Argument | Type | Meaning |
|---|---|---|
charIdOrRef | number|table | Character id or reference table. |
exports.jrs_core:GetHealthStateForSource(source)GetHealthState for the character an online player is using.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
Create And Delete Hooks#
Hooks let your resource react to a new or deleted character without editing jrs_core. Each hook has a name, so registering the same name again replaces the old one.
exports.jrs_core:RegisterCharacterStarterHook(name, callback)Runs your function for every newly created character. Errors are caught and printed, they do not stop other hooks.
exports.jrs_core:RegisterCharacterStarterHook('jrs_myscript', function(source, charId)
exports.jrs_inventory:addItem(source, 'canned_beans', 3)
end)| Argument | Type | Meaning |
|---|---|---|
name | string | Unique hook name, usually your resource name. |
callback | function | Called as callback(source, charId). |
exports.jrs_core:RegisterCharacterDeleteHook(name, callback)Runs your function when a character is deleted, so you can clean up your own tables. Return false from the callback to report a failure.
exports.jrs_core:RegisterCharacterDeleteHook('jrs_myscript', function(charId, identifier, phase)
MySQL.update('DELETE FROM my_wagons WHERE charid = ?', { charId })
end)| Argument | Type | Meaning |
|---|---|---|
name | string | Unique hook name. |
callback | function | Called as callback(charId, identifier, phase). |
exports.jrs_core:RunCharacterDeleteHooks(charId, identifier, phase?)Runs every registered delete hook. The character selector calls this, you only need it if you delete characters from your own tool.
| Argument | Type | Meaning |
|---|---|---|
charId | number | Character id. |
identifier | string | Owner account identifier. |
phase | string | Default before. |
exports.jrs_core:RunStarterHooks(source, charId)Runs every starter hook, then fires jrs_core:server:firstCharacterSetup. Normally called by the creator.
| Argument | Type | Meaning |
|---|---|---|
source | number | Player server id. |
charId | number | Character id. |
exports.jrs_character:IsCharacterSoftDeleted(charId)Characters deleted from the selector can be kept as soft deleted for a while. This tells you if one is.
| Argument | Type | Meaning |
|---|---|---|
charId | number | Character id. |
exports.jrs_character:RestoreDeletedCharacter(charId)Brings a soft deleted character back. It does not check permissions, so guard it in your own code.
| Argument | Type | Meaning |
|---|---|---|
charId | number | Character id. |
State Bags#
jrs_core writes these keys on the player state bag when the character is ready and sets them to false when it is unloaded. They replicate to every client, so other players can read them too.
| Key | Type | Content |
|---|---|---|
CharacterReady |
boolean | true while a character is active |
CharacterId |
number | character id |
CharacterIdentifier |
string | owner account identifier |
FirstName, LastName |
string | character name |
Nickname |
string | nickname, may be empty |
Gender |
string | gender from the creator |
Job, JobGrade, JobLabel |
string, number, string | active job |
OnDuty |
boolean | duty flag |
Group |
string | permission group, for example user or admin |
-- client: your own character
if LocalPlayer.state.CharacterReady then
print(LocalPlayer.state.FirstName, LocalPlayer.state.Job)
end
-- server: any player
local job = Player(source).state.Job
-- client: react when the ready flag flips
AddStateBagChangeHandler('CharacterReady', ('player:%s'):format(GetPlayerServerId(PlayerId())), function(_, _, value)
if value then print('character ready') end
end)State bags are a fast read only copy. To change a job or a name use the exports above, never state:set from your own script.