在 lua 中函数只返回一个值而不是两个值

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

我正在尝试 return name 和 name2 的值,但是 return 只给出了第一个值。我是 lua 的新手,一般都在写代码,所以只是想学习

调用此函数的其他地方:

   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)

本地名称 1,名称 2 = GetCharactersName()

有两个可能的原因。

  1. 名称 2 为零
  2. 您正在以将 return 值的数量调整为 1 的方式使用函数调用。发生这种情况 GetCharacterName(source) 不是表达式列表中的最后一个,或者如果您将括号中的函数调用或将其分配给单个变量。

编辑:

现在您已经添加了如何使用该函数的示例:

local charname = GetCharacterName(usource)

在这种情况下,GetCharacterName 的结果列表被调整为 1 个值(名称),因为您只分配给一个变量。如果您想要两个 return 值,则需要两个变量。

要么这样做:

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

那么 charName 就是 "Trump, Donald" 例如。

或者您 return 该名称作为单个字符串:

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

那么local charname = GetCharacterName(usource)就可以了