搜尋

首頁  >  問答  >  主體

如何將參數從客戶端傳遞到伺服器回調,將資料儲存在數組中,然後將其發送回客戶端

我想將車輛變數從客戶端發送到伺服器回調,在那裡,我將驗證具有steam id的人是否擁有他試圖打開的車輛,如果他擁有,那麼所有的他擁有的車牌將被保存在一個數組中,並發送回客戶端。

如何將參數從客戶端傳遞到伺服器回呼?

#客戶端

function openmenuvehicle()
    local playerPed = PlayerPedId()
    local coords = GetEntityCoords(playerPed)
    local vehicle = nil

    if IsPedInAnyVehicle(playerPed, false) then
        vehicle = GetVehiclePedIsIn(playerPed, false)
    else
        vehicle = getVehicleInDirection(3.0)

        if not DoesEntityExist(vehicle) then
            vehicle = GetClosestVehicle(coords, 3.0, 0, 70)
        end
    end


    if DoesEntityExist(vehicle) then
        local lockStatus = GetVehicleDoorLockStatus(vehicle)
        if lockStatus == 0 or lockStatus == 1 then
            local trunkpos = GetWorldPositionOfEntityBone(vehicle, GetEntityBoneIndexByName(vehicle, "boot"))
            local distanceToTrunk = GetDistanceBetweenCoords(coords, trunkpos, 1)

            if distanceToTrunk <= 1.25 or (trunkpos.x + trunkpos.y + trunkpos.z) == 0.0 then

                ESX.TriggerServerCallback('esx_inventoryhud_trunk:getOwner', function(isOwner)
                print(isOwner)

                    if isOwner then 
                        TriggerEvent(
                        "mythic_progbar:client:progress",
                        {
                            name = "Open_Trunk",
                            duration = Config.OpenTime,
                            label = 'ABRINDO MALA',
                            useWhileDead = false,
                            canCancel = true,
                            controlDisables = {
                                disableMovement = true,
                                disableCarMovement = true,
                                disableMouse = false,
                                disableCombat = true
                            }
                        },
                        function(status)
                            if not status then
                                currentVehicle = vehicle
                                
                                SetVehicleDoorOpen(vehicle, 5, false, false)
                                local class = GetVehicleClass(vehicle)
                                OpenCoffreInventoryMenu(GetVehicleNumberPlateText(vehicle), Config.VehicleLimit[class])
                            end
                        end
                        )
                    end
                end)
            else
                exports['okokNotify']:Alert("", "Aproxima-te da mala", 3000, 'error')
            end
        else
            exports['okokNotify']:Alert("", "Mala trancada", 3000, 'error')
        end
    else
        exports['okokNotify']:Alert("", "Sem veículos por perto", 3000, 'error') 
    end
end

#伺服器端

ESX.RegisterServerCallback("esx_inventoryhud_trunk:getOwner", function(source, cb, plate)

    local id = GetPlayerIdentifiers(source)[1]
    
    MySQL.Async.fetchAll("SELECT plate FROM owned_vehicles WHERE owner = @owner", {['@owner'] = id}, function(data)
        if data[1].owner == id then 
            return cb(true)
        else
            return cb(false)
        end
    end)
end)

P粉785905797P粉785905797230 天前426

全部回覆(1)我來回復

  • P粉567281015

    P粉5672810152024-04-04 12:07:24

    您所需要做的就是在客戶端的回調中新增一個附加參數。如果您像我下面那樣定義上面的板,那麼“,板”就可以了。

    local plate = GetVehicleNumberPlateText(vehicle)
    ESX.TriggerServerCallback('esx_inventoryhud_trunk:getOwner', function(isOwner, owned_vehicles)
        print(isOwner)
    
        if isOwner then
            TriggerEvent(
                "mythic_progbar:client:progress",
                {
                    name = "Open_Trunk",
                    duration = Config.OpenTime,
                    label = 'ABRINDO MALA',
                    useWhileDead = false,
                    canCancel = true,
                    controlDisables = {
                    disableMovement = true,
                    disableCarMovement = true,
                    disableMouse = false,
                    disableCombat = true
                }
            },
            function(status)
                if not status then
                    currentVehicle = vehicle
    
                    SetVehicleDoorOpen(vehicle, 5, false, false)
                    local class = GetVehicleClass(vehicle)
                    OpenCoffreInventoryMenu(plate, Config.VehicleLimit[class])
                end
            end)
        end
    end, plate)

    對於伺服器端,您只需 cb(data)。

    ESX.RegisterServerCallback("esx_inventoryhud_trunk:getOwner", function(source, cb, plate)
    
        local id = GetPlayerIdentifiers(source)[1]
    
        MySQL.Async.fetchAll("SELECT plate FROM owned_vehicles WHERE owner = @owner", {['@owner'] = id}, function(data)
            local found = false;
            for i = 1, #data do
                local this = data[i]
                if this.plate == plate then
                    cb(true, data)
                    found = true; -- need to do this to prevent to callbacks
                end
            end
            if not found then cb(false, {}) end
        end)
    end)

    回覆
    0
  • 取消回覆