[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 = {

"airbus",
"airtug",
"akuma",
"annihilator",
"armytanker",
"armytrailer",
"armytrailer2",
"asea",
"asea2",
"asterope",
"bagger",
"baletrailer",
"baller",
"baller2",
"barracks",
"barracks2",
"bati",
"bati2",
"benson",
"bfinjection",
"biff",
"bison",
"bison2",
"bison3",
"bjxl",
"blazer",
"blazer2",
"blazer3",
"blimp", 
"blista",
"bmx",
"boattrailer",
"bobcatxl",
"bodhi2",
"boxville",
"boxville2",
"boxville3",
"buccaneer",
"buffalo",
"buffalo2",
"bulldozer",
"bullet",
"burrito",
"burrito2",
"burrito3", 
"burrito4",
"burrito5",
"bus",
"buzzard",
"buzzard2",
"cablecar",
"caddy",
"caddy2",
"camper",
"carbonizzare",
"carbonrs",
"cargobob",
"cargobob2",
"cargobob3",
"cargoplane",
"cavalcade",
"cavalcade2",
"coach",
"cogcabrio",
"coquette",
"cruiser",
"crusader",
"cuban800",
"cutter",
"daemon",
"dilettante",
"dilettante2",
"dinghy",
"dinghy2",
"dloader",
"docktrailer",
"docktug",
"dominator",
"double",
"dubsta",
"dubsta2",
"dump",
"dune",
"dune2",
"duster",
"emperor",
"emperor2",
"emperor3",
"entityxf",
"exemplar",
"f620",
"felon2",
"firetruk", 
"fixter",
"flatbed", 
"forklift",
"fq2", 
"freight",
"freightcar",
"freightcont1",
"freightcont2",
"freightgrain",
"freighttrailer",
"frogger",
"frogger2",
"fugitive",
"fusilade",
"futo",
"gauntlet",
"gburrito",
"graintrailer",
"granger",
"gresley",
"habanero",
"handler",
"hauler",
"hexer",
"hotknife",
"infernus",
"ingot",
"intruder",
"issi2",
"jackal",
"jb700",
"jet",
"jetmax",
"journey",
"khamelion",
"landstalker",
"lazer",
"lguard",
"luxor",
"mammatus",
"manana",
"marquis",
"maverick",
"mesa",
"mesa2",
"mesa3",
"metrotrain",
"minivan",
"mixer",
"mixer2",
"monroe",
"mower",
"mule",
"mule2",
"nemesis",
"ninef2",
"packer",
"patriot",
"pbus",
"pcj",
"penumbra",
"peyote",
"phantom",
"phoenix",
"picador",
"pony",
"pony2",
"pounder",
"predator",
"premier",
"primo",
"proptrailer",
"radi",
"raketrailer",
"rancherxl",
"rancherxl2",
"rapidgt",
"rapidgt2",
"ratloader",
"rebel", 
"rebel2",
"regina",
"rentalbus",
"rhino",
"ripley",
"rocoto",
"rubble",
"ruffian",
"ruiner",
"rumpo",
"rumpo2",
"sabregt",
"sadler",
"sadler2",
"sanchez",
"sanchez2",
"sandking",
"sandking2",
"schafter2",
"schwarzer",
"scorcher",
"scrap",
"seashark",
"seashark2",
"seminole",
"sentinel2",
"serrano",
"shamal",
"skylift",
"speedo",
"speedo2",
"squalo",
"stanier",
"stingergt",
"stockade",
"stockade3",
"stratum",
"stretch",
"stunt",
"submersible",
"sultan",
"suntrap",
"superd",
"surano",
"surfer",
"surfer2",
"surge",
"taco",
"tailgater",
"tanker",
"tankercar",
"tiptruck",
"tiptruck2",
"titan",
"tornado",
"tornado2",
"tornado3",
"tornado4",
"tourbus",
"towtruck",
"towtruck2",
"tr2",
"tr3",
"tr4",
"tractor",
"tractor2",
"tractor3",
"trflat",
"tribike",
"tribike2",
"tribike3",
"tropic",
"tvtrailer",
"utillitruck",
"utillitruck2",
"utillitruck3",
"vacca",
"vader",
"velum",
"vigero",
"voltic",
"voodoo2",
"washington",
"youga",
"zion2",
"ztype"
}

-- CODE --

Citizen.CreateThread(function()
	while true do
		Wait(1)

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

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

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

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

	return false
end

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.

2 Likes

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

RegisterServerEvent(‘baseevents:enteringVehicle’)

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.