[Help] Ped Flee Vehicle Script

Hey, everybody I’ve been working on a script recently for our server so that players can steal cars from peds with a weapon. The script is suppose to work so when you aim your gun at the localped in vehicle, they step out, and put their hands up. you would then be able to rob them for their keys which would take about 5 seconds. the other idea is if you dont have a gun, you could try at opening a car door with a localped in it. there would be a 1/3 chance of opening the car (the localped left it “unlocked”). once the door opens, you rip them out normally and enter the car to “look for the keys” (another 1/3 success rate) for 5 seconds. the police would either be notified when you start looking for the keys, or when you start “robbing” the localped for their keys. we currently cant figure out the “give keys” function between players, so including that would be of great help with making this work im sure!

At the moment, in the current development stage for this script, i’ve manged to get it so when player is aiming gun at ped, they stop vehicle and leave there vehicle and put there hands up (Sort of).

I was stuck on the script at a certain point as ussually the NPC’s would jus hop out and flee instantly without displaying an animation, so I found a work around so when NPC exits car it performs TaskPedWander (Or some syntax like) so now the ped will stand there for 8 seconds then flee after timer, but adding that task now causes another problem of where the ped instantly TP’s out of the vehicle and the animation of hands up, only shows for a blink second at the begin and end of the Citizen.Wait()

Was just wondering if anybody more experienced than I am could help me out of this script, not too experienced my self, just starting learning Lua so some help would be very much appreciated.

Video of code in action: https://streamable.com/jf1b6
Current Stage Of Code:

Citizen.CreateThread(function()
local lastent = nil
local lastveh = nil
local talking = false
while true do
Citizen.Wait(0)
local pid = PlayerId()
local foundaiment, aiment = GetEntityPlayerIsFreeAimingAt(pid)
if IsPlayerFreeAiming(pid) and foundaiment and (lastent~=aiment or IsPedInAnyVehicle(aiment, false)) and (Config.car_aim_flee_range==0 or GetDistanceBetweenCoords(GetEntityCoords(aiment), GetEntityCoords(GetPlayerPed(-1)), false)<=Config.car_aim_flee_range) then
lastent=aiment
– local x,y,z = table.unpack(GetEntityCoords(aiment))
– DrawBox(x+1,y+1,z+1,x-1,y-1,z-1, 0, 255, 0, 200)
local vehicle = GetVehiclePedIsIn(aiment, false)
lastveh = vehicle
if DoesEntityExist(vehicle) and IsEntityAVehicle(vehicle) and DoesEntityExist(aiment) and IsEntityAPed(aiment) and not IsPedAPlayer(aiment) then
TaskLeaveVehicle(aiment, vehicle, 46)
while IsPedInAnyVehicle(aiment,false) do Citizen.Wait(250); TaskLeaveVehicle(aiment, vehicle, 46)
ClearPedTasksImmediately(aiment)
Citizen.Wait(1)
SetPedCombatAttributes(aiment, 46, true)
SetPedFleeAttributes(aiment, 0, 0)
SetBlockingOfNonTemporaryEvents(aiment, true)
TaskPlayAnim(aiment,“random@mugging3”, “handsup_standing_base”, 0, 0, -1, 49, 0, 0, 0, 0 )
TaskWanderInArea(aiment, 0, 0, 0, 20, 100, 100)
ResetPedLastVehicle(aiment)
Citizen.Wait(7500)
ClearPedTasksImmediately(aiment)
TaskReactAndFleePed(aiment, PlayerPedId())
end
end
end
end
end)

EDIT: See here for a better version: [Help] Ped Flee Vehicle Script

Cleaned it up and tweaked it a bit, I cannot test it at the moment.

Hello, ChristopherM, thank you very much for looking at the script and re-writing certain bits, but now when script is active when player points gun at ped they jus freak out and drive away.

Thanks, Haze

I’ll have a look when I get home, see if I can get it to work; I only have my (Mac) laptop at the moment :+1:

1 Like

SetEntityAsMissionEntity(aiment) to get better control of them, then start doing your tasks/animations, waits, etc. It’s best to call this at the very beginning as soon as you’ve collected their handle and ready to do something with them.

When you’re finished do SetEntityAsNoLongerNeeded(aiment) and they’ll walk away. You can task them to flee, wait a few seconds and then call that, it just makes sure the game will clean up and despawn them when they’re out of sight.

1 Like

Ok, so it took a few hours, but I got it working. Honestly, it was a lot of fun to make, thanks for giving me something to do lol.

Demo video

3 Likes

Hey ChristopherM, thank you very much for having another look, fixing and adding a new feature to my script, I appropriate the help very much! I can declare that the script works and is now usable for everybody for our server! I added a little syntax on HasKeys = False
to make it undrivable/locked so if they dont get the keys they can’t use the vehicle!

Again thank you very much for taking your time out of your day to help out with this!
And I’m happy you enjoyed coding this and getting it to work, because I very much didn’t XD
-Haze

1 Like

All you were missing in the original script was the loading the animation dictionary, other than that it would have worked, I just took some creative liberty and added to it :+1:

1 Like

Going to want to do a few more checks on the IsPedArmed - to make sure you can’t use a flashlight to force someone out of the vehicle. Could have a look here and the part that makes the ped get out of the car - set a flag that ped is a driver… and when you do the mugging if isdriver = true then do % chance to get keys.

My script also has alot of functions already built in for notify police, and has option to make player use voice to be able to start mugging instead of hitting E.

SpikE

hey christopher , i tried your ped flee script but i didn’t got any notification about i got the keys or not and they there is no animation about dropping keys , can you please tell me what to do

The snippet still works fine for me, so I’d say copy the code and try again.

Can you install a animation in key dropping by npc so that we know that they gave the keys

This is only a snippet, not a release; I don’t plan on providing very much support for it past basic trouble shooting, and I don’t plan on adding any new features.

Okay thanks

can u add like a process bar ?

is there a chance the locals will leave the car without aiming at them?