I’m developing a mode where the player can ride as a passenger in an NPC’s vehicle, and I’m encountering some issues

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>-- 근처 차량 탐색 함수
function GetNearestVehicle(coords, radius)
local vehicles = GetVehiclesInArea(coords, radius)
local nearestVehicle = nil
local nearestDistance = radius
for _, vehicle in ipairs(vehicles) do
local vehicleCoords = GetEntityCoords(vehicle)
local distance = #(coords - vehicleCoords)
if distance < nearestDistance then
nearestVehicle = vehicle
nearestDistance = distance
end
end
return nearestVehicle
end
-- 특정 반경 내의 모든 차량을 가져오는 함수
function GetVehiclesInArea(coords, radius)
local vehicles = {}
local handle, vehicle = FindFirstVehicle()
local success
repeat
local vehicleCoords = GetEntityCoords(vehicle)
local distance = #(coords - vehicleCoords)
if distance <= radius then
table.insert(vehicles, vehicle)
end
success, vehicle = FindNextVehicle(handle)
until not success
EndFindVehicle(handle)
return vehicles
end
-- 주변 차량의 빈 좌석 확인 함수 (운전석 제외)
function GetEmptySeats(vehicle)
local emptySeats = {}
local maxPassengers = GetVehicleMaxNumberOfPassengers(vehicle)
for seat = 0, maxPassengers - 1 do
if IsVehicleSeatFree(vehicle, seat) then
table.insert(emptySeats, seat)
end
end
return emptySeats
end
-- 특정 좌석의 좌표를 얻는 함수
function GetSeatCoords(vehicle, seatIndex)
local pedInSeat = GetPedInVehicleSeat(vehicle, seatIndex)
if pedInSeat == 0 then -- If the seat is empty, get its coordinates
local offset = GetOffsetFromEntityInWorldCoords(vehicle, 0.0, 0.0, 0.0)
if seatIndex == 0 then
offset = GetOffsetFromEntityInWorldCoords(vehicle, 0.5, 1.0, 0.0) -- Front passenger seat
elseif seatIndex == 1 then
offset = GetOffsetFromEntityInWorldCoords(vehicle, -0.5, -1.0, 0.0) -- Rear left seat
elseif seatIndex == 2 then
offset = GetOffsetFromEntityInWorldCoords(vehicle, 0.5, -1.0, 0.0) -- Rear right seat
end
return offset
end
return nil
end
-- 플레이어와 가장 가까운 좌석을 찾는 함수 (운전석 제외)
function GetClosestSeat(playerCoords, vehicle)
local emptySeats = GetEmptySeats(vehicle)
local closestSeat = nil
local closestDistance = math.huge
for _, seat in ipairs(emptySeats) do
local seatCoords = GetSeatCoords(vehicle, seat)
if seatCoords then
local distance = #(playerCoords - seatCoords)
if distance < closestDistance then
closestSeat = seat
closestDistance = distance
end
end
end
return closestSeat
end
-- 차량 내 모든 NPC를 우호적으로 설정하는 함수
function SetVehiclePedsFriendly(vehicle, playerPed)
local maxPassengers = GetVehicleMaxNumberOfPassengers(vehicle)
for seat = -1, maxPassengers - 1 do
local ped = GetPedInVehicleSeat(vehicle, seat)
if ped ~= 0 then
SetPedAsNoLongerNeeded(ped)
SetPedRelationshipGroupHash(ped, GetHashKey("PLAYER"))
TaskSetBlockingOfNonTemporaryEvents(ped, true)
SetPedFleeAttributes(ped, 0, 0)
SetPedCombatAttributes(ped, 17, 1)
end
end
end
-- 명령어 등록
RegisterCommand("testC", function()
local playerPed = PlayerPedId()
-- 플레이어가 이미 차량에 탑승한 상태인지 확인
if IsPedInAnyVehicle(playerPed, false) then
return
end
local playerCoords = GetEntityCoords(playerPed)
local radius = 5.0 -- 반경 5미터
local nearestVehicle = GetNearestVehicle(playerCoords, radius)
if nearestVehicle then
local closestSeat = GetClosestSeat(playerCoords, nearestVehicle)
if closestSeat then
-- 차량 소유권을 클라이언트 측에서 변경
SetEntityAsMissionEntity(nearestVehicle, true, true)
SetVehicleHasBeenOwnedByPlayer(nearestVehicle, true)
SetVehicleNeedsToBeHotwired(nearestVehicle, false)
SetPedCanBeDraggedOut(playerPed, false)
SetVehicleDoorsLocked(nearestVehicle, 1)
SetVehiclePedsFriendly(nearestVehicle, playerPed)
TaskEnterVehicle(playerPed, nearestVehicle, -1, closestSeat, 1.5, 1, 0)
-- 탑승 후 강제로 좌석을 유지하도록 설정
Citizen.Wait(3000) -- 차량 탑승 애니메이션이 끝날 때까지 잠시 대기
TaskWarpPedIntoVehicle(playerPed, nearestVehicle, closestSeat)
else
print("빈 좌석이 없습니다.")
end
else
print("반경 내에 차량이 없습니다.")
end
end, false)
RegisterKeyMapping('testC', 'test', 'keyboard', 'g')
</code>
<code>-- 근처 차량 탐색 함수 function GetNearestVehicle(coords, radius) local vehicles = GetVehiclesInArea(coords, radius) local nearestVehicle = nil local nearestDistance = radius for _, vehicle in ipairs(vehicles) do local vehicleCoords = GetEntityCoords(vehicle) local distance = #(coords - vehicleCoords) if distance < nearestDistance then nearestVehicle = vehicle nearestDistance = distance end end return nearestVehicle end -- 특정 반경 내의 모든 차량을 가져오는 함수 function GetVehiclesInArea(coords, radius) local vehicles = {} local handle, vehicle = FindFirstVehicle() local success repeat local vehicleCoords = GetEntityCoords(vehicle) local distance = #(coords - vehicleCoords) if distance <= radius then table.insert(vehicles, vehicle) end success, vehicle = FindNextVehicle(handle) until not success EndFindVehicle(handle) return vehicles end -- 주변 차량의 빈 좌석 확인 함수 (운전석 제외) function GetEmptySeats(vehicle) local emptySeats = {} local maxPassengers = GetVehicleMaxNumberOfPassengers(vehicle) for seat = 0, maxPassengers - 1 do if IsVehicleSeatFree(vehicle, seat) then table.insert(emptySeats, seat) end end return emptySeats end -- 특정 좌석의 좌표를 얻는 함수 function GetSeatCoords(vehicle, seatIndex) local pedInSeat = GetPedInVehicleSeat(vehicle, seatIndex) if pedInSeat == 0 then -- If the seat is empty, get its coordinates local offset = GetOffsetFromEntityInWorldCoords(vehicle, 0.0, 0.0, 0.0) if seatIndex == 0 then offset = GetOffsetFromEntityInWorldCoords(vehicle, 0.5, 1.0, 0.0) -- Front passenger seat elseif seatIndex == 1 then offset = GetOffsetFromEntityInWorldCoords(vehicle, -0.5, -1.0, 0.0) -- Rear left seat elseif seatIndex == 2 then offset = GetOffsetFromEntityInWorldCoords(vehicle, 0.5, -1.0, 0.0) -- Rear right seat end return offset end return nil end -- 플레이어와 가장 가까운 좌석을 찾는 함수 (운전석 제외) function GetClosestSeat(playerCoords, vehicle) local emptySeats = GetEmptySeats(vehicle) local closestSeat = nil local closestDistance = math.huge for _, seat in ipairs(emptySeats) do local seatCoords = GetSeatCoords(vehicle, seat) if seatCoords then local distance = #(playerCoords - seatCoords) if distance < closestDistance then closestSeat = seat closestDistance = distance end end end return closestSeat end -- 차량 내 모든 NPC를 우호적으로 설정하는 함수 function SetVehiclePedsFriendly(vehicle, playerPed) local maxPassengers = GetVehicleMaxNumberOfPassengers(vehicle) for seat = -1, maxPassengers - 1 do local ped = GetPedInVehicleSeat(vehicle, seat) if ped ~= 0 then SetPedAsNoLongerNeeded(ped) SetPedRelationshipGroupHash(ped, GetHashKey("PLAYER")) TaskSetBlockingOfNonTemporaryEvents(ped, true) SetPedFleeAttributes(ped, 0, 0) SetPedCombatAttributes(ped, 17, 1) end end end -- 명령어 등록 RegisterCommand("testC", function() local playerPed = PlayerPedId() -- 플레이어가 이미 차량에 탑승한 상태인지 확인 if IsPedInAnyVehicle(playerPed, false) then return end local playerCoords = GetEntityCoords(playerPed) local radius = 5.0 -- 반경 5미터 local nearestVehicle = GetNearestVehicle(playerCoords, radius) if nearestVehicle then local closestSeat = GetClosestSeat(playerCoords, nearestVehicle) if closestSeat then -- 차량 소유권을 클라이언트 측에서 변경 SetEntityAsMissionEntity(nearestVehicle, true, true) SetVehicleHasBeenOwnedByPlayer(nearestVehicle, true) SetVehicleNeedsToBeHotwired(nearestVehicle, false) SetPedCanBeDraggedOut(playerPed, false) SetVehicleDoorsLocked(nearestVehicle, 1) SetVehiclePedsFriendly(nearestVehicle, playerPed) TaskEnterVehicle(playerPed, nearestVehicle, -1, closestSeat, 1.5, 1, 0) -- 탑승 후 강제로 좌석을 유지하도록 설정 Citizen.Wait(3000) -- 차량 탑승 애니메이션이 끝날 때까지 잠시 대기 TaskWarpPedIntoVehicle(playerPed, nearestVehicle, closestSeat) else print("빈 좌석이 없습니다.") end else print("반경 내에 차량이 없습니다.") end end, false) RegisterKeyMapping('testC', 'test', 'keyboard', 'g') </code>
-- 근처 차량 탐색 함수
function GetNearestVehicle(coords, radius)
    local vehicles = GetVehiclesInArea(coords, radius)
    local nearestVehicle = nil
    local nearestDistance = radius

    for _, vehicle in ipairs(vehicles) do
        local vehicleCoords = GetEntityCoords(vehicle)
        local distance = #(coords - vehicleCoords)
        if distance < nearestDistance then
            nearestVehicle = vehicle
            nearestDistance = distance
        end
    end

    return nearestVehicle
end

-- 특정 반경 내의 모든 차량을 가져오는 함수
function GetVehiclesInArea(coords, radius)
    local vehicles = {}
    local handle, vehicle = FindFirstVehicle()
    local success

    repeat
        local vehicleCoords = GetEntityCoords(vehicle)
        local distance = #(coords - vehicleCoords)
        if distance <= radius then
            table.insert(vehicles, vehicle)
        end
        success, vehicle = FindNextVehicle(handle)
    until not success

    EndFindVehicle(handle)
    return vehicles
end

-- 주변 차량의 빈 좌석 확인 함수 (운전석 제외)
function GetEmptySeats(vehicle)
    local emptySeats = {}
    local maxPassengers = GetVehicleMaxNumberOfPassengers(vehicle)

    for seat = 0, maxPassengers - 1 do
        if IsVehicleSeatFree(vehicle, seat) then
            table.insert(emptySeats, seat)
        end
    end

    return emptySeats
end

-- 특정 좌석의 좌표를 얻는 함수
function GetSeatCoords(vehicle, seatIndex)
    local pedInSeat = GetPedInVehicleSeat(vehicle, seatIndex)
    if pedInSeat == 0 then -- If the seat is empty, get its coordinates
        local offset = GetOffsetFromEntityInWorldCoords(vehicle, 0.0, 0.0, 0.0)

        if seatIndex == 0 then
            offset = GetOffsetFromEntityInWorldCoords(vehicle, 0.5, 1.0, 0.0) -- Front passenger seat
        elseif seatIndex == 1 then
            offset = GetOffsetFromEntityInWorldCoords(vehicle, -0.5, -1.0, 0.0) -- Rear left seat
        elseif seatIndex == 2 then
            offset = GetOffsetFromEntityInWorldCoords(vehicle, 0.5, -1.0, 0.0) -- Rear right seat
        end

        return offset
    end

    return nil
end

-- 플레이어와 가장 가까운 좌석을 찾는 함수 (운전석 제외)
function GetClosestSeat(playerCoords, vehicle)
    local emptySeats = GetEmptySeats(vehicle)
    local closestSeat = nil
    local closestDistance = math.huge

    for _, seat in ipairs(emptySeats) do
        local seatCoords = GetSeatCoords(vehicle, seat)
        if seatCoords then
            local distance = #(playerCoords - seatCoords)
            if distance < closestDistance then
                closestSeat = seat
                closestDistance = distance
            end
        end
    end

    return closestSeat
end

-- 차량 내 모든 NPC를 우호적으로 설정하는 함수
function SetVehiclePedsFriendly(vehicle, playerPed)
    local maxPassengers = GetVehicleMaxNumberOfPassengers(vehicle)
    for seat = -1, maxPassengers - 1 do
        local ped = GetPedInVehicleSeat(vehicle, seat)
        if ped ~= 0 then
            SetPedAsNoLongerNeeded(ped)
            SetPedRelationshipGroupHash(ped, GetHashKey("PLAYER"))
            TaskSetBlockingOfNonTemporaryEvents(ped, true)
            SetPedFleeAttributes(ped, 0, 0)
            SetPedCombatAttributes(ped, 17, 1)
        end
    end
end

-- 명령어 등록
RegisterCommand("testC", function()
    local playerPed = PlayerPedId()

    -- 플레이어가 이미 차량에 탑승한 상태인지 확인
    if IsPedInAnyVehicle(playerPed, false) then
        return
    end

    local playerCoords = GetEntityCoords(playerPed)
    local radius = 5.0 -- 반경 5미터

    local nearestVehicle = GetNearestVehicle(playerCoords, radius)
    if nearestVehicle then
        local closestSeat = GetClosestSeat(playerCoords, nearestVehicle)

        if closestSeat then
            -- 차량 소유권을 클라이언트 측에서 변경
            SetEntityAsMissionEntity(nearestVehicle, true, true)
            SetVehicleHasBeenOwnedByPlayer(nearestVehicle, true)
            SetVehicleNeedsToBeHotwired(nearestVehicle, false)
            SetPedCanBeDraggedOut(playerPed, false)
            SetVehicleDoorsLocked(nearestVehicle, 1)

            SetVehiclePedsFriendly(nearestVehicle, playerPed)
            TaskEnterVehicle(playerPed, nearestVehicle, -1, closestSeat, 1.5, 1, 0)

            -- 탑승 후 강제로 좌석을 유지하도록 설정
            Citizen.Wait(3000) -- 차량 탑승 애니메이션이 끝날 때까지 잠시 대기
            TaskWarpPedIntoVehicle(playerPed, nearestVehicle, closestSeat)

        else
            print("빈 좌석이 없습니다.")
        end
    else
        print("반경 내에 차량이 없습니다.")
    end
end, false)


RegisterKeyMapping('testC', 'test', 'keyboard', 'g')

Here is the client code for the content mentioned.
It works very well, but when riding as a passenger in the NPC’s vehicle,
the player gets out of the car by themselves after a short while.
I have handled the vehicle’s ownership and the relationship with the passenger,
but the problem remains unchanged.
How can I solve this?

I have handled the vehicle’s ownership and the relationship with the passenger

New contributor

Violet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật