如何将参数从客户端传递到服务器回调,将数据存储在数组中,然后将其发送回客户端
P粉785905797
P粉785905797 2024-04-03 13:05:29
0
1
476

我想将车辆变量从客户端发送到服务器回调,在那里,我将验证具有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粉785905797
P粉785905797

全部回复(1)
P粉567281015

您所需要做的就是向客户端的回调添加一个附加参数。如果您像我下面那样定义上面的板,那么“,板”就可以了。

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)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板