I want to register some character attributes to a database (things like money, cars and so on)
I thought a good way is doing it by getting his steam ID. How do i do that / whats the command to get a players steam id?
If there is no good way to do it, what do you think how I could save data of players the best way?
You can use GetPlayerIdentifiers(âŚ) https://runtime.fivem.net/doc/natives/#_0x7302DBCF
It returns a table with every identifiers in it (license, steam, discord âŚ).
Use it server side with source in its arguments.
I looked for an example, I found one, but in this there are 3 IDs, SteamID, license and ip
Is there a way to use only the steam id ?
I dont want 3 lines in my mysql table only for know which player it is
function ExtractIdentifiers()
local identifiers = {
steam = "",
ip = "",
discord = "",
license = "",
xbl = "",
live = ""
}
--Loop over all identifiers
for i = 0, GetNumPlayerIdentifiers(source) - 1 do
local id = GetPlayerIdentifier(source, i)
--Convert it to a nice table.
if string.find(id, "steam") then
identifiers.steam = id
elseif string.find(id, "ip") then
identifiers.ip = id
elseif string.find(id, "discord") then
identifiers.discord = id
elseif string.find(id, "license") then
identifiers.license = id
elseif string.find(id, "xbl") then
identifiers.xbl = id
elseif string.find(id, "live") then
identifiers.live = id
end
end
return identifiers
end
-- Usage:
local identifiers = ExtractIdentifiers()
local steam = identifiers.steam
print(steam)
Maybe not ideal, fastest or most convenient way of doing this, but it works.
Do not that if a identifier is not present, it will becomed "" aka an empty string.
Maybe noteworthy, there are also plans to extract a specific identifiers via a native. No sure how far that has progressed or it would be implemented soon.
Error loading script server.lua in resource meinmod: Execution of native 00000000ff7f66ab in script host failed.
stack traceback:
[C]: in upvalue â_inâ
citizen:/scripting/lua/natives_server.lua:150: in function âGetNumPlayerIdentifiersâ
server.lua:12: in function âExtractIdentifiersâ
server.lua:36: in main chunk
Failed to load script server.lua.
InvokeNatcfx> ive: execution failed: Argument at index 0 was null.
Error loading script server.lua in resource get_ids: Execution of native 00000000ff7f66ab in script host failed.
stack traceback:
[C]: in upvalue '_in'
citizen:/scripting/lua/natives_server.lua:150: in function 'GetNumPlayerIdentifiers'
server.lua:15: in function 'ExtractIdentifiers'
server.lua:39: in main chunk
Failed to load script server.lua.
Started resource get_ids
I see what happened, you must put it in an event, otherwise source is nil.
This is how I use it:
RegisterServerEvent("tp-identity:request_id")
AddEventHandler("tp-identity:request_id", function()
local ids = ExtractIdentifiers()
local identity = FetchIdentity(ids)
--...Rest of server authentication code...
end)
LoadResourceFile("server_ids", "data/identifiers.json")
function ExtractIdentifiers()
local identifiers = {
steam = "",
ip = "",
discord = "",
license = "",
xbl = "",
live = ""
}
--Loop over all identifiers
for i = 0, GetNumPlayerIdentifiers(source) - 1 do
local id = GetPlayerIdentifier(source, i)
--Convert it to a nice table.
if string.find(id, "steam") then
identifiers.steam = id
elseif string.find(id, "ip") then
identifiers.ip = id
elseif string.find(id, "discord") then
identifiers.discord = id
elseif string.find(id, "license") then
identifiers.license = id
elseif string.find(id, "xbl") then
identifiers.xbl = id
elseif string.find(id, "live") then
identifiers.live = id
end
end
return identifiers
end
-- Usage:
RegisterCommand("printids", function()
TriggerEvent("rhys19:request_id")
LoadResourceFile("server_ids", "data/identifiers.json")
end)
RegisterServerEvent("rhys19:request_id")
AddEventHandler("rhys19:request_id", function()
local identifiers = ExtractIdentifiers()
local steam = identifiers.steam
SaveResourceFile(GetCurrentResourceName(), "data/identifiers.json", json.encode(ExtractIdentifiers()), -1)
print("Saved Steam Identifier to data/identifiers.json")
end)