How to get Steam ID by Code?

mysql
help
lua

#1

Hey guys,

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?


#2

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.


#3

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


#4

This how I use it in my server:

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.


#5

So this is the way how you print it in your mySQL database right?
Im new to mySQL, sorry if this might be a dumb question


#6

Uhm no, this just converts it to a convenient table.


#7

and the print method will show it in the console then?


#8

Correct. It will be empty is steam is not present.


#9

when i use your code it shows me error for “GetNumPlayerIdentifiers(source)”


#10

What is the error? Ensure that you are using this server-side also.


#11

also you could just store it in a json file couldn’t you?


#12

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.

This is what my console said


#13

yeah i seem to be receiving the same issue

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

#14

Make sure you run this function on the server, native definitely exists:
https://runtime.fivem.net/doc/natives/#_0xFF7F66AB


#15

i put this in server.lua and this is what i got in my __resource.lua:
server_script ‘server.lua’
so this should be serversided


#16

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)

#17

ah now it is working :smiley: for me at least now to make it insert to a json file


#18

@rhys19 Easy as this:

SaveResourceFile(GetCurrentResourceName(), "id.json",  json.encode(ExtractIdentifiers()), -1)

#19

it’s not wanting to print my steam id or anything it just wants to stay empty but it does do this

{"xbl":"","steam":"","discord":"","ip":"","live":"","license":""}

#20

Server.lua

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)