[HELP] Ped gets out of car after animation (and should not)

I’m developing my personal carthief script based on loffe_carthief, but i have a strange problem.
I make the ped enter the vehicle, do the mechanic animation for a certain amount of time keeping the vehicle off, and then the vehicle starts and player can use it, the problem is that sometimes after the animation ends the player gets out of the vehicle for no reason. Someone knows how to fix this?

Here’s the code of the problematic part:

Summary

if GetDistanceBetweenCoords(coords, Config.PickPoints[pickup].x, Config.PickPoints[pickup].y, Config.PickPoints[pickup].z, true) < 50 and stop ~= 1 then
if spawned_cars == nil then
spawned_cars = CreateVehicle(vehicleModel, Config.PickPoints[pickup].x, Config.PickPoints[pickup].y, Config.PickPoints[pickup].z, Config.PickPoints[pickup].h, true, false)
if spawned_cars ~= nil then
print("Car spawned at x: " … Config.PickPoints[pickup].x … " y: " … Config.PickPoints[pickup].y … " z: " … Config.PickPoints[pickup].z)
end
end
–while ped gets on board triggers the lockpicking
while not IsPedInVehicle(player, spawned_cars, false) and stop ~= 1 do
Citizen.Wait(100)
end
if stop ~= 1 then
local vehicle = GetVehiclePedIsIn(player, false)
local vehicleLabel = GetDisplayNameFromVehicleModel(Config.carModels[carNum])
local streetName = GetLabelText((GetNameOfZone(coords.x, coords.y, coords.z)))
TriggerServerEvent(‘esx_outlawalert:carthiefInProgress’, coords, streetName, vehicleLabel)
TriggerServerEvent(‘ProjectX_CondorThief:updateThefts’)
RequestAnimDict(‘anim@amb@clubhouse@tutorial@bkr_tut_ig3@’)
while not HasAnimDictLoaded(‘anim@amb@clubhouse@tutorial@bkr_tut_ig3@’) do
Citizen.Wait(0)
end
–turn off engine and then turn it on after waiting lockpicking amount
–thread for keeping vehicle engine off while lockpicking
local vehOff = true
Citizen.CreateThread(function ()
while vehOff do
Citizen.Wait(0)
SetVehicleEngineOn(vehicle, false, false, false)
SetVehicleUndriveable(vehicle, false, false, false)
end
end)
Citizen.Wait(1000)
TaskPlayAnim(player, ‘anim@amb@clubhouse@tutorial@bkr_tut_ig3@’ , ‘machinic_loop_mechandplayer’, 8.0, 8.0, Config.lockpickingTime, 17, 0, false, false, false )
SetVehicleAlarm(vehicle, true)
SetVehicleAlarmTimeLeft(vehicle, Config.lockpickingTime)
Citizen.Wait(Config.lockpickingTime)
vehOff = false

Are you using in somewhere related with this part, a ClearPedTaks?

Nop, i removed them bc i figured out that they could be the cause. But still not resolved

1 Like

Have not looked at the code, but keep the the ClearPedTasks and you can add a TaskLeaveVehicle which targets the ped driver and vehicle. You can use code below or check the docs for usage.

local pedDriver = GetPedInVehicleSeat(vehicle, -1)
TaskLeaveVehicle(pedDriver, vehicle, 0)

It’s the opposite, i don’t want the player to leave the vehicle automatically. I want the ped to stay in the vehicle, but instead it gets out by itself sometimes

local pedDriver = GetPedInVehicleSeat(vehicle, -1)
local rngNum = math.random(0, 100)

if rngNum > 80 and not IsPedAPlayer(pedDriver) then
TaskLeaveVehicle(pedDriver, vehicle, 0)
end

the code above is 20% chance to trigger and only if ped is not a player
the TaskLeaveVehicle 0 value can be changed to any of the values below.

Also the math.random will only work on server side lua files

0 = normal exit and closes door.
1 = normal exit and closes door.
16 = teleports outside, door kept closed.
64 = normal exit and closes door, maybe a bit slower animation than 0.
256 = normal exit but does not close the door.
4160 = ped is throwing himself out, even when the vehicle is still.
262144 = ped moves to passenger seat first, then exits normally
Others to be tried out: 320, 512, 131072.

I think i did not explain myself so well.
The ped ALREADY exits the car by himself, without telling him to do so. I want him to NOT leave the car after the animation ends

I had worked on a script that had similar bug, the ped would also get out on his own and walk away. The issue was that I was calling SetVehicleDoorsLockedForAllPlayers on the vehicle that had a npc ped instead of a player, and that caused the ped to exit the vehicle.