0
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)
4

2 回答 2

2

有两个可能的原因。

  1. name2 为零
  2. 您正在以将返回值的数量调整为 1 的方式使用函数调用。这GetCharacterName(source)不是表达式列表中的最后一个,或者如果您将该函数调用放在括号中或将其分配给单个变量。

编辑:

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

local charname = GetCharacterName(usource)

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

要么这样做:

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

然后 charName 是"Trump, Donald"例如。

或者您将该名称作为单个字符串返回:

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-14T18:52:24.880 回答
0

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

于 2021-05-14T18:51:59.307 回答