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

我想返回 name 和 name2 两个值,但是实际上只返回了第一个值。我是 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)
点赞
用户15928757
用户15928757
local name1, name2 = GetCharactersName()

将以上代码翻译为中文并保留原本的markdown格式:

`local name1, name2 = GetCharactersName()`

`local name1` 和 `name2` 是两个变量名,它们将从 `GetCharactersName()` 函数中获取到的角色名称赋给它们。
2021-05-14 18:51:59
用户2858170
用户2858170

有两种可能的原因。

  1. name2 是nil
  2. 您正在以调整返回值数为1的方式使用函数调用。当GetCharacterName(source)不是表达式列表中的最后一个或将该函数调用放入括号中或将其分配给单个变量时,就会发生这种情况。

编辑:

既然你已经添加了关于如何使用该函数的示例:

local charname = GetCharacterName(usource)

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

要么像这样做:

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

然后charName是例如“特朗普,唐纳德”。

要么你将该名称作为单个字符串返回:

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)将工作。

2021-05-14 18:52:24