Player and character data retrieval using the DataStore pattern
The Fetch component is the primary way to access player and character data in Mythic Framework. It retrieves DataStore objects that provide methods to get and set data for players and characters.
Critical: Do NOT use COMPONENTS.Characters:GetCharacter() - this method does not exist. Always use Fetch:Source() or Fetch:SID() to access character data.
Mythic Framework uses a DataStore pattern for managing player and character state:
Copy
-- Get player DataStorelocal player = Fetch:Source(source)-- Get character DataStore from playerlocal character = player:GetData('Character')-- Access character datalocal stateId = character:GetData('SID')local firstName = character:GetData('First')
Get player DataStore by server source ID.Parameters:
Name
Type
Required
Description
source
number
Yes
Player server source
Returns:
Type
Description
DataStore/nil
Player DataStore object or nil if not found
Example:
Copy
-- Server sidelocal player = Fetch:Source(source)if player then print('Account ID:', player:GetData('AccountID')) print('Player Name:', player:GetData('Name')) print('Identifier:', player:GetData('Identifier')) -- Get character local char = player:GetData('Character') if char then print('Character Name:', char:GetData('First'), char:GetData('Last')) endend
-- Server sidelocal player = Fetch:PlayerData('AccountID', 12345)if player then print('Found player by account ID') local source = player:GetData('Source')end
-- Server sidelocal players = Fetch:All()for source, player in pairs(players) do print(source, player:GetData('Name')) local char = player:GetData('Character') if char then print(' Character:', char:GetData('First'), char:GetData('Last')) endend
-- Server sidelocal player = Fetch:CharacterData('Phone', '555-0123')if player then local char = player:GetData('Character') print('Found character:', char:GetData('First'), char:GetData('Last'))end
-- Server sidelocal player = Fetch:SID(123)if player then local char = player:GetData('Character') print('Character:', char:GetData('First'), char:GetData('Last')) -- Send notification local source = player:GetData('Source') TriggerClientEvent('mythic-notifications:client:Send', source, { message = 'You received a notification', type = 'info' })end
Get next player in iteration (for loops).Parameters:
Name
Type
Required
Description
prev
number
Yes
Previous source (0 for first)
Returns:
Type
Description
DataStore/nil
Next player DataStore or nil if end
Example:
Copy
-- Server side - Iterate through all playerslocal player = Fetch:Next(0)while player do local char = player:GetData('Character') if char then print(char:GetData('First'), char:GetData('Last')) end player = Fetch:Next(player:GetData('Source'))end
Get specific data from offline character (database query).Parameters:
Name
Type
Required
Description
stateId
number
Yes
Character SID
key
string
Yes
Field to retrieve
Returns:
Type
Description
any
Requested field value or nil
Example:
Copy
-- Server side (blocking/synchronous)local phone = Fetch:GetOfflineData(123, 'Phone')print('Offline character phone:', phone)-- Get multiple fieldslocal cash = Fetch:GetOfflineData(123, 'Cash')local bank = Fetch:GetOfflineData(123, 'Bank')
GetOfflineData is synchronous and blocks the thread. Use sparingly and only when player is offline.
-- Get specific fieldlocal firstName = character:GetData('First')local stateId = character:GetData('SID')-- Get all datalocal allData = character:GetData()print(json.encode(allData, {indent = true}))
-- Server sideAddEventHandler('myresource:server:DoSomething', function() local src = source local player = Fetch:Source(src) if not player then print('Player not found') return end local char = player:GetData('Character') if not char then TriggerClientEvent('mythic-notifications:client:Send', src, { message = 'You must be logged in as a character', type = 'error' }) return end -- Access character data local stateId = char:GetData('SID') local cash = char:GetData('Cash') local job = char:GetData('Job') -- Modify character data char:SetData('Cash', cash - 100) -- Success TriggerClientEvent('mythic-notifications:client:Send', src, { message = 'Action completed', type = 'success' })end)
-- Server sideAddEventHandler('phone:server:CallNumber', function(phoneNumber) local src = source local caller = Fetch:Source(src):GetData('Character') -- Find recipient by phone number local recipient = Fetch:CharacterData('Phone', phoneNumber) if recipient then local recipientChar = recipient:GetData('Character') local recipientSource = recipient:GetData('Source') -- Start call TriggerClientEvent('phone:client:IncomingCall', recipientSource, { from = caller:GetData('Phone'), name = string.format('%s %s', caller:GetData('First'), caller:GetData('Last')) }) else TriggerClientEvent('phone:client:Notification', src, 'Number not in service') endend)
-- Server sideAddEventHandler('events:server:TriggerServerWideEvent', function(eventName, eventData) local players = Fetch:All() for source, player in pairs(players) do local char = player:GetData('Character') if char then -- Send event to all characters TriggerClientEvent('events:client:ServerWideEvent', source, eventName, eventData) -- Give reward local currentCash = char:GetData('Cash') char:SetData('Cash', currentCash + 1000) end end print(string.format('Event sent to %d characters', Fetch:CountCharacters()))end)
local player = Fetch:Source(source)if not player then return endlocal char = player:GetData('Character')if not char then TriggerClientEvent('mythic-notifications:client:Send', source, { message = 'You must be logged in', type = 'error' }) returnend-- Safe to use char now
-- You have a character SID, need the player sourcelocal player = Fetch:SID(stateId)if player then local source = player:GetData('Source') -- Use source for TriggerClientEvent, etc.end
function IsCharacterOnline(stateId) return Fetch:SID(stateId) ~= nilend-- Usageif IsCharacterOnline(123) then print('Character is online')else print('Character is offline')end
DataStore: In-memory state for online players/characters (fast)
Database: Persistent storage (slower, for offline access)
Use Fetch methods for online players (instant)
Use Database queries for offline players (requires callback)
Player vs Character
Player: Account-level data (AccountID, Identifier, Name)
Character: In-game character (SID, First, Last, Cash, Bank)Players can have multiple characters. Always get Character from Player:
Copy
local player = Fetch:Source(source)local char = player:GetData('Character') -- May be nil!
SetData Persistence
SetData updates in-memory DataStore only. Characters auto-save to database every ~10 minutes.For critical data, manually save: