[REQUEST] Script that locks 50% of NPC AI vehicles?

So far I have only come across scripts that lock ALL AI vehicles. Does anyone know if there is one for say 50% of AI vehicles chance to be locked? Both parked and driving? This would make car jacking a lot more risky (say someone is on the run) and just more immersive since most cars IRL lock when you go above a certain speed anyways.

Looking for non-esx - standalone.

I’m aware there are some lockpick scripts. But that is silly. If you are car jacking a moving vehicle sitting there trying to hotwire or lockpick it makes no sense when someone was… just driving it lol.

Thank you all!

Can you post the script that locks all AI vehicles? It’ll probably be possible to modify the script the way you want to.

There was this but its super old and apparently broken. Plus its a lock pick script which isnt the same thing. It also uses .net .dll file which cant be edited easily. Sigh.

Everything else is ESX. Frustrating.

Try something like this:

--- server

-- if not using lua5.4, you may need this
math.randomseed(os.time())

AddEventHandler('entityCreated', function(entity)
    local entityType = GetEntityType(entity)

    -- not a vehicle? bail out
    if entityType ~= 2 then return end

    -- only for 'random' population cars
    if GetEntityPopulationType(entity) > 5 then return end

    -- if >0.5 (50% chance of locking), bail out
    if math.random() > 0.5 then return end

    -- lock!
    SetVehicleDoorsLocked(entity, 2)
end)
8 Likes

would i just put this into a server.lua in a directory and ensure the resource (sorry im new to scripting resources) or was this an addition to the above script

entityCreated is a server-side native, so yes it would go into the server.lua!
You can check out Server events - Cfx.re Docs

2 Likes

This is working nicely so far! Thank you so much!

One question, if you guys know… is it possible to create an if statement where say if the window is broken (such as someone shoots the driver and breaks the window) that you can reach in and unlock (just like the animation when you break a window while stealing a parked car and then reach in - minus the breaking of the window with your elbow part) or is that not possible?

This may be something done with another lock value.

SCRIPT ERROR: Execution of native 000000000b1bd08d in script host failed: Tried to access invalid entity: 198485

any idea what to do about this

b1db08d is GetEntityType - Natives @ Cfx.re Docs. The error means that by the time GetEntityType() ran, the entity was not valid anymore (maybe deleted?)

We can try to check if the vehicle exists first, before running the event code, like this:

--- server

-- if not using lua5.4, you may need this
math.randomseed(os.time())

AddEventHandler('entityCreated', function(entity)
    --not valid anymore? skedaddle
    if not DoesEntityExist(entity) then return end 

    --[[  rest of core remains the same  ]]
    local entityType = GetEntityType(entity)

    -- not a vehicle? bail out
    if entityType ~= 2 then return end

    -- only for 'random' population cars
    if GetEntityPopulationType(entity) > 5 then return end

    -- if >0.5 (50% chance of locking), bail out
    if math.random() > 0.5 then return end

    -- lock!
    SetVehicleDoorsLocked(entity, 2)
end)
2 Likes

Native functions i.e: GetEntityPoplationType() do not work in a server file.

Any ideas how to adjust to something else

If you mean adjust as in another native that would work server side, then none. There are only a few natives that work server sided most don’t really interact with players, more for external data fetching. Take a look here: Native Reference - Cfx.re Docs

This native exists and works on the server fine. Note that it’s ‘GetEntityPopulationType’, not ‘Poplation’ - did you forget the u when testing, too?

Even the docs say ‘API set: server’.

Hahaha, I didn’t test anything but when I typed here it was like 5 am about 30 seconds before I fell asleep. Yeah probably should have checked but the general point I was trying to get at was most natives only work on the client side with a select few that work on the server side as well (again check docs like I should have hahaha)