[HELP] Lock some cars on the Servers - CODE HELP

The code is working fine.
But only one problem. Me and the players are all lagged. ( FPS FROPS ) 20 FPS…
The owner of this code is: @pongo1231 , i only changed one our two things
I think the problem is the proximity thing.
Can someone try to fix?

-- CONFIG --

-- Blacklisted vehicle models
carblacklist = {


-- CODE --

	while true do

		playerPed = GetPlayerPed(-1)
		if playerPed then
			checkCar(GetVehiclePedIsIn(playerPed, false))

			x, y, z = table.unpack(GetEntityCoords(playerPed, true))
			for _, blacklistedCar in pairs(carblacklist) do
				checkCar(GetClosestVehicle(x, y, z, 10.0, GetHashKey(blacklistedCar), 70))

function checkCar(car)
	if car then
		carModel = GetEntityModel(car)
		carName = GetDisplayNameFromVehicleModel(carModel)

		if isCarBlacklisted(carModel) then
			SetVehicleDoorsLocked(car, 2)

function isCarBlacklisted(model)
	for _, blacklistedCar in pairs(carblacklist) do
		if model == GetHashKey(blacklistedCar) then
			return true

	return false

You have wait 1, then check the players car against the blacklist, then look for every blacklist car near him, and you wonder why it’s lagging? Every frame you’re doing this? So 30-60-90 times a second? With 10 people that’s 600 times a second?

And if you’re in a car, why would the doors suddenly lock? Shouldn’t it be cars that are empty? Even then, you drive a car, get out, it magically locks?

Anyway, change it to Wait(5000). Every 5 seconds, every play runs this code, that’s plenty. Maybe even a bigger wait.


thank you for the explanation

Although sometimes I get into the cars because it only does every (5000) the check.
But it seems to me to be the only solution.

Ahh okay good point, if you run to the car before it runs the script. If it lags at 1, does it lag at 1000?

You could use the standard baseevents.

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


is started by default. You might be able to interrupt that and lock the blacklisted car. Or check if they press the F key and run the check.