Hello, I try to get a value in the DB but I’m not able to get the result, can someone tell me what is my mistake please ? I don’t see it.
RegisterServerEvent('Keep_cool:getStatForce')
AddEventHandler('Keep_cool:getStatForce', function()
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
MySQL.Async.execute('SELECT * FROM users WHERE identifier = "@identifier"',
{
['@identifier'] = xPlayer.identifier,
},
function(result)
local user = result[1]
local force = user['force']
TriggerClientEvent('Keep_cool:setStatForce', force)
end)
end)
RegisterServerEvent('Keep_cool:getStatEndurance')
AddEventHandler('Keep_cool:getStatEndurance', function()
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
MySQL.Async.execute('SELECT * FROM users WHERE identifier = "@identifier"',
{
['@identifier'] = xPlayer.identifier,
},
function(result)
local user = result[1]
local endurance = user['endurance']
TriggerClientEvent('Keep_cool:setStatEndurance', endurance)
end)
end)
result is null due to a MySQL error, so you are referencing a value that does not exist. Try removing the quotes around “identifier” in your MySQL query string. So something like this:
MySQL.Async.execute('SELECT * FROM users WHERE identifier = @identifier',
{
['@identifier'] = xPlayer.identifier,
},
function(result)
local user = result[1]
local force = user['force']
TriggerClientEvent('Keep_cool:setStatForce', force)
end)
In addition, you could reduce the code to one function to get both stats…
RegisterServerEvent('Keep_cool:getStats')
AddEventHandler('Keep_cool:getStats', function()
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
MySQL.Async.execute('SELECT * FROM users WHERE identifier = @identifier',
{
['@identifier'] = xPlayer.identifier,
},
function(result)
--This if statement will prevent error is result is nil
if result ~= nil then
local user = result[1]
TriggerClientEvent('Keep_cool:setStatEndurance', user['endurance'])
TriggerClientEvent('Keep_cool:setStatForce', user['force'])
end
end)
end)
And, lastly, ensure you are getting the ESX shared object prior to using ESX functions by having this on the top of your script: