<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.