luareturnfivem

function only returning one value instead of both in lua


function GetCharacterName(source)
  local xPlayer = QBCore.Functions.GetPlayer(source)
  local name = xPlayer.PlayerData.charinfo.lastname
  local name2 = xPlayer.PlayerData.charinfo.firstname
    if xPlayer then
     return name, name2
    end
  end

I'm trying to return the values of name and name2, however the return is only giving the first value. I'm newer to lua, and code in general, so just trying to learn

Additional places in which this function is called:

   AddEventHandler("mdt:attachToCall", function(index)
   local usource = source
   local charname = GetCharacterName(usource)
   local xPlayers = QBCore.Functions.GetPlayers()
   for i= 1, #xPlayers do
       local source = xPlayers[i]
       local xPlayer = QBCore.Functions.GetPlayer(source)
       if xPlayer.PlayerData.job.name == 'police' then
           TriggerClientEvent("mdt:newCallAttach", source, index, charname)
       end
   end
   TriggerClientEvent("mdt:sendNotification", usource, "You have attached to this call.")
end)
RegisterServerEvent("mdt:performVehicleSearchInFront")
AddEventHandler("mdt:performVehicleSearchInFront", function(query)
    local usource = source
    local xPlayer = QBCore.Functions.GetPlayer(usource)
    if xPlayer.job.name == 'police' then
        exports['ghmattimysql']:execute("SELECT * FROM (SELECT * FROM `mdt_reports` ORDER BY `id` DESC LIMIT 3) sub ORDER BY `id` DESC", {}, function(reports)
            for r = 1, #reports do
                reports[r].charges = json.decode(reports[r].charges)
            end
            exports['ghmattimysql']:execute("SELECT * FROM (SELECT * FROM `mdt_warrants` ORDER BY `id` DESC LIMIT 3) sub ORDER BY `id` DESC", {}, function(warrants)
                for w = 1, #warrants do
                    warrants[w].charges = json.decode(warrants[w].charges)
                end
                exports['ghmattimysql']:execute("SELECT * FROM `player_vehicles` WHERE `plate` = @query", {
                    ['@query'] = query
                }, function(result)
                    local officer = GetCharacterName(usource)
                    TriggerClientEvent('mdt:toggleVisibilty', usource, reports, warrants, officer, xPlayer.job.name)
                    TriggerClientEvent("mdt:returnVehicleSearchInFront", usource, result, query)
                end)
            end)
        end)
    end
end)

Solution

  • There are two possible reasons.

    1. name2 is nil
    2. you're using the function call in a way that adjusts the number of return values to 1. This happens GetCharacterName(source) is not the last in a list of expressions or if you put that function call into parenthesis or you assign it to a single variable.

    Edit:

    Now that you've added examples of how you use that function:

    local charname = GetCharacterName(usource)
    

    In this case the result list of GetCharacterName is ajdusted to 1 value (name) as you only assingn to a single variable. If you want both return values you need two variables.

    Either do it like so:

    local lastName, firstName = GetCharactername(usource)
    local charName = lastName .. ", " .. firstname
    

    Then charName is "Trump, Donald" for example.

    Or you return that name as a single string:

    function GetCharacterName(source)
      local xPlayer = QBCore.Functions.GetPlayer(source)
      local name = xPlayer.PlayerData.charinfo.lastname
      local name2 = xPlayer.PlayerData.charinfo.firstname
        if xPlayer then
         return name .. ", " .. name2
        end
      end
    

    Then local charname = GetCharacterName(usource) will work