1
0
mirror of https://github.com/citizenfx/cfx-server-data.git synced 2025-01-11 00:03:18 +08:00
cfx-server-data/resources/[system]/baseevents/vehiclechecker.lua

54 lines
2.0 KiB
Lua
Raw Normal View History

2016-12-15 20:40:07 +08:00
local isInVehicle = false
local isEnteringVehicle = false
local currentVehicle = 0
local currentSeat = 0
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
local ped = PlayerPedId()
if not isInVehicle and not IsPlayerDead(PlayerId()) then
if DoesEntityExist(GetVehiclePedIsTryingToEnter(ped)) and not isEnteringVehicle then
-- trying to enter a vehicle!
local vehicle = GetVehiclePedIsTryingToEnter(ped)
local seat = GetSeatPedIsTryingToEnter(ped)
isEnteringVehicle = true
TriggerServerEvent('baseevents:enteringVehicle', vehicle, seat, GetDisplayNameFromVehicleModel(GetEntityModel(vehicle)))
elseif not DoesEntityExist(GetVehiclePedIsTryingToEnter(ped)) and not IsPedInAnyVehicle(ped, true) and isEnteringVehicle then
-- vehicle entering aborted
TriggerServerEvent('baseevents:enteringAborted')
isEnteringVehicle = false
elseif IsPedInAnyVehicle(ped, false) then
-- suddenly appeared in a vehicle, possible teleport
isEnteringVehicle = false
isInVehicle = true
currentVehicle = GetVehiclePedIsUsing(ped)
currentSeat = GetPedVehicleSeat(ped)
local model = GetEntityModel(currentVehicle)
local name = GetDisplayNameFromVehicleModel()
TriggerServerEvent('baseevents:enteredVehicle', currentVehicle, currentSeat, GetDisplayNameFromVehicleModel(GetEntityModel(currentVehicle)))
end
elseif isInVehicle then
if not IsPedInAnyVehicle(ped, false) or IsPlayerDead(PlayerId()) then
-- bye, vehicle
local model = GetEntityModel(currentVehicle)
local name = GetDisplayNameFromVehicleModel()
TriggerServerEvent('baseevents:leftVehicle', currentVehicle, currentSeat, GetDisplayNameFromVehicleModel(GetEntityModel(currentVehicle)))
isInVehicle = false
currentVehicle = 0
currentSeat = 0
end
end
Citizen.Wait(50)
end
end)
function GetPedVehicleSeat(ped)
local vehicle = GetVehiclePedIsIn(ped, false)
for i=-2,GetVehicleMaxNumberOfPassengers(vehicle) do
if(GetPedInVehicleSeat(vehicle, i) == ped) then return i end
end
return -2
end