Hi, i need to download the specific job of a person when he login. I’m not able to find the “targetPlayer” parameter that “TriggerClientEvent” request me.
server.lua

local function OnPlayerConnecting(name, setKickReason, deferrals)
	local steamID = nil
	local identifiers = GetPlayerIdentifiers(source)
	
	--Steam identifier
	for _, v in pairs(identifiers) do
        if string.find(v, "steam") then
            steamID = v
            break
		end
    end
	
	MySQL.ready(function ()
		MySQL.Async.fetchAll("SELECT * FROM `users` WHERE `identifier`='@steam'", {['steam'] = steamID}, function(player)
			TriggerClientEvent("mysqlTest:test", (?)targetPlayer(?), player[1].name, player[1].job)
		end)
	end)
    
end

AddEventHandler("playerConnecting", OnPlayerConnecting)

client.lua

local job = nil

RegisterNetEvent("mysqlTest:test")
AddEventHandler("mysqlTest:test", function(par1, par2)
	job = par1
end)

RegisterCommand("test", function(source, args, rawCommand)
	TriggerEvent('chat:addMessage', {
		args = { "Your job is : " .. job }
	})
end, false)

Please do not tell me to execute the query when i do “/test” command because i need to show a marker only if he has a specific job. So “/test” command will not exists. Thanks in advance

source

not work

Under your variables declaration put :

local Source = source

And put Source in your TriggerClientEvent

My server.lua becomed like this

local function OnPlayerConnecting(name, setKickReason, deferrals)
	local steamID = nil
	local identifiers = GetPlayerIdentifiers(source)
	local Source = source
	
	print("Source: " .. source)
	
	--Steam identifier
	for _, v in pairs(identifiers) do
        if string.find(v, "steam") then
            steamID = v
            break
		end
    end
	
	MySQL.ready(function ()
		MySQL.Async.fetchAll("SELECT * FROM `users` WHERE `identifier`=@steam", {['steam'] = steamID}, function(player)
			TriggerClientEvent("mysqlTest:test", Source, "test", "test")
		end)
	end)
    
end

AddEventHandler("playerConnecting", OnPlayerConnecting)

But not work

I am not sure but I had this before where I couldn’t do triggerclientevent in a mysql function handler. Try it outside by setting the data you need to a variable or something.

Yes, possible !

i tryed like this but not work… :frowning:

local function OnPlayerConnecting(name, setKickReason, deferrals)
	local steamID = nil
	local identifiers = GetPlayerIdentifiers(source)
	
	print("Source: " .. source)
	
	--Steam identifier
	for _, v in pairs(identifiers) do
        if string.find(v, "steam") then
            steamID = v
            break
		end
    end
	
	MySQL.ready(function ()
		MySQL.Async.fetchAll("SELECT * FROM `users` WHERE `identifier`=@steam", {['steam'] = steamID}, function(player)
			
		end)
	end)
	TriggerClientEvent("mysqlTest:test", source, "test", "test")
end

AddEventHandler("playerConnecting", OnPlayerConnecting)

do you have a client event in your client.lua called the above?

yeah, client.lua

local job = nil

RegisterNetEvent("mysqlTest:test")
AddEventHandler("mysqlTest:test", function(par1, par2)
	job = par1
end)

RegisterCommand("test", function(source, args, rawCommand)
	TriggerEvent('chat:addMessage', {
		args = { "Your job is : " .. job }
	})
end, false)

resource.lua

resource_manifest_version "44febabe-d386-4d18-afbe-5e627f4af937"

client_script "client.lua"

server_script {
	"@mysql-async/lib/MySQL.lua",
	"server.lua"
}

dependency "mysql-async"

and when you do /test it doesnt show “Your job is : test”?

RegisterCommand("test", function(source, args, rawCommand)
	print("Comando /test catturato")
	TriggerEvent('chat:addMessage', {
		args = { "Your job is : " .. job}
	})
end, false)

local function OnPlayerConnecting(name, setKickReason, deferrals)
  MySQL.ready(function()
    local steamID = nil
    local identifiers = GetPlayerIdentifiers(source)

    print("Source: " .. source)

    for _, v in pairs(identifiers) do
      if string.find(v, "steam") then
        steamID = v
        break
      end
    end

    local result = MySQL.Sync.fetchAll("SELECT * FROM `users` WHERE `identifier`=@steam", {['steam'] = steamID})
    if result then
      TriggerClientEvent("mysqlTest:test", source, result)
    end
  end)
end

Try MySQL.Sync.fetchAll("SELECT… to get that working because thats how it works for me

And I am gonna continue looking but I do not see why it doesn’t work

local function OnPlayerConnecting

Could it not be because of this. It only does it when you connect try restarting your game and the joining.

Put print here :

if result then
   print(result[1].id)
   TriggerClientEvent("mysqlTest:test", source, result)
end

There is a result in server console ?

With your code i get this error.

cfx> Connecting: MinePuzza
InvokeNative: execution failed: Argument at index 0 was null.
Error resuming coroutine: Execution of native 00000000ff7f66ab in script host failed.
stack traceback:
        [C]: in upvalue '_in'
        citizen:/scripting/lua/natives_server.lua:169: in function 'GetNumPlayerIdentifiers'
        citizen:/scripting/lua/scheduler.lua:244: in function 'GetPlayerIdentifiers'
        @mysqlTest/server.lua:4: in upvalue 'callback'
        @mysql-async/lib/MySQL.lua:197: in function <@mysql-async/lib/MySQL.lua:189>

Line 4 of server.lua is

local identifiers = GetPlayerIdentifiers(source)

So maybe into Mysql.ready the parameter source is not set

I tihnk it is the fact that it triggers on game connecting. He needs to fully restart game to see if it works

The problem is not the result of the query, i think. Cause if i put a static string the triggerclientevent not work

and it still doesn’t work on restart of game at all?