NearestVehicle error

yoshishi

Новичок
Автор темы
15
1
Версия MoonLoader
.027.0-preview
How do I fix this error in this script?

(error) closestVehicle.lua: opcode '09B3' call caused an unhandled exception
stack traceback:
[C]: in function 'getCarDoorLockStatus'
... st\Otomoos\GTA San Andreas\moonloader\closestVehicle.lua:27: in function <... st\Otomoos\GTA San Andreas\moonloader\closestVehicle.lua:5>

(error) closestVehicle.lua: opcode '00AA' call caused an unhandled exception
stack traceback:
[C]: in function 'getCarCoordinates'
... st\Otomoos\GTA San Andreas\moonloader\closestVehicle.lua:31: in function <... st\Otomoos\GTA San Andreas\moonloader\closestVehicle.lua:5>


Lua:
script_description("This script finds and marks the nearest unoccupied car that the player can approach.")
function main()
    while not isSampAvailable() do
        wait(200)
    end
-- Register the chat command "/nc"
    sampRegisterChatCommand("nc", function()
        local ped = PLAYER_PED
-- damn it if the player is inside the vehicle
        if isCharInAnyBoat(ped) or isCharInAnyCar(ped) or isCharInAnyHeli(ped)
           or isCharInAnyPlane(ped) or isCharOnAnyBike(ped) then
sampAddChatMessage("You can't use this command while inside the vehicle.", -1347440726)
            return
        end
-- Check if the player is indoors
        if getActiveInterior() ~= 0 then
sampAddChatMessage("You can't use this command here.", -1347440726)
            return
        end
        local nearestVehicle = nil
        local vehicle, _ = storeClosestEntities(ped)
        local isOccupied = false
-- Go through all the players to check if the nearest vehicle is occupied.
        for PlayerID = 0, sampGetMaxPlayerId(false) do
            local result, playerped = sampGetCharHandleBySampPlayerId(PlayerID)
            if result then
-- Check if the player is inside the nearest vehicle
                if isCharInCar(playerped, vehicle) then
                    isOccupied = true
                    break
                end
            end
        end
-- Check if the nearest car is empty and if its doors are unlocked.
        if not isOccupied and getCarDoorLockStatus(vehicle) == 0 then
            nearestVehicle = vehicle
        end
-- If a nearby unoccupied vehicle is found, mark it.
        if nearestVehicle then
            local vehicleX, vehicleY, vehicleZ = getCarCoordinates(nearestVehicle)
-- Remove existing flash (if any)
            removeBlip(checkpoint)
-- Create a new tag for the nearest car
            checkpoint = addBlipForCar(nearestVehicle)
            changeBlipColour(checkpoint, 0x355E3BFF)
-- Create a flow to wait for the player to get close to the vehicle.
            lua_thread.create(function()
                repeat
                    wait(0)
                    local playerX, playerY, playerZ = getCharCoordinates(ped)
                until getDistanceBetweenCoords3d(vehicleX, vehicleY, vehicleZ, playerX, playerY, playerZ) < 3.0 or not doesBlipExist(checkpoint)
-- Remove the mark as soon as it is reached and play the sound.
                removeBlip(checkpoint)
                addOneOffSound(0, 0, 0, 1149)
            end)
-- Instruct the player to follow the checkpoint to the nearest vehicle.
sampAddChatMessage("Follow from the checkpoint to the nearest car.", 0xFFFFFF)
        end
    end)
    while true do
        wait(0)
    end
end
 

quesada

q-team
Проверенный
892
1,292
try this script
shit code:
local search = false;
local vehicleTarget;

function main()
    while not isSampAvailable() do wait(0) end

    sampRegisterChatCommand('nc',
    function()
        if search then
            removeBlip(checkpoint)
            sampAddChatMessage('disabled', -1)
            search = false;
            return
        end

        if isCharInAnyCar(PLAYER_PED) or getActiveInterior() ~= 0 then
            sampAddChatMessage('Sorry :(', -1)
            return
        end

        if getCar(100, false, false) then

            vehicleTarget = getCar(100, false, false)
            vehicleHandle = select(2, sampGetCarHandleBySampVehicleId(vehicleTarget))

            if getCarDoorLockStatus(vehicleHandle) == 0 then
                CAR_POS = {getCarCoordinates(vehicleHandle)}
                checkpoint = addBlipForCar(vehicleHandle)
                changeBlipColour(checkpoint, 0x355E3BFF)
                search = true;
            else
                sampAddChatMessage('No open car!', -1)
            end
        else
            sampAddChatMessage('Not found car', -1)
        end

    end)

    while true do wait(0)
        if search then
            local PLAYER_POS = {getCharCoordinates(PLAYER_PED)}
            local DISTANCE = getDistanceBetweenCoords3d(CAR_POS[1], CAR_POS[2], CAR_POS[3], PLAYER_POS[1], PLAYER_POS[2], PLAYER_POS[3])

            if DISTANCE > 3 then
                printStringNow(string.format('Distance: ~r~%d', DISTANCE), 1000)
            else
                removeBlip(checkpoint)
                addOneOffSound(0, 0, 0, 1149)
                sampAddChatMessage('yes bro!!', -1)
                search = false;
            end

        end
    end
end

function getCar(checkDist, passenger, driver)
    local dist, id = 9999, -1
    local PLAYER_POS = {getCharCoordinates(PLAYER_PED)}
    
    for k, veh in pairs(getAllVehicles()) do
        local _, vid = sampGetVehicleIdByCarHandle(veh)
        if _ then
            local driverCar = getDriverOfCar(veh) ~= -1
            if (not driver and not driverCar) or (driver and driverCar) then
                local CAR_POS = {getCarCoordinates(veh)}
                local NEW_DISTANCE = getDistanceBetweenCoords3d(CAR_POS[1], CAR_POS[2], CAR_POS[3], PLAYER_POS[1], PLAYER_POS[2], PLAYER_POS[3])
                
                if (not passenger or getMaximumNumberOfPassengers(veh) > 0) and NEW_DISTANCE < checkDist and dist > NEW_DISTANCE then
                    id = vid
                    dist = NEW_DISTANCE
                end
            end
        end
    end
    
    return id ~= -1 and id or false
end