[SOLVED] Adding AI wanted level to ESX

If you don’t use it, I would suggest you remove it as it just unnecessarily complicates the check line.

i will try this

-- Disable wanted level
firstLoop = 'yes'
Citizen.CreateThread(function()
	while true do
		if firstLoop then
			Citizen.Wait(10000)
		else
			Citizen.Wait(0)
		end
		if ((Config.DisableWantedLevel or copCount >= Config.EnoughCopsOnline) == false wantedToggle ~= 'on') or copCount <= Config.EnoughCopsOnline (wantedToggle == 'off' == false) or PlayerData.job.name == 'police' then
			local playerId = PlayerId()
			if GetPlayerWantedLevel(playerId) ~= 0 then
				SetPlayerWantedLevel(playerId, 0, false)
				SetPlayerWantedLevelNow(playerId, false)
			end
		end
		firstLoop = nil
	end
end)

Editt don’t work

wantedToggle == 'off' == false

You butchered the line pretty well. Example above is stating "if wanted toggle equals off equals false.

I would like the Wanted Level of the NPC police to be active only if no LSPD player is present and when an LSPD player is present the wanted level deactivates it to let the LSPD players do their jobs

i try this but when i have Police job and i shoot in the street i got Wanted level

-- Disable wanted level
firstLoop = 'yes'
Citizen.CreateThread(function()
	while true do
		if firstLoop then
			Citizen.Wait(10000)
		else
			Citizen.Wait(0)
		end
		if ((Config.DisableWantedLevel or copCount >= Config.EnoughCopsOnline) and playerId == false and wantedToggle ~= 'on') or (wantedToggle == 'off' and playerId == false) or PlayerData.job.name == 'police' then
			local playerId = PlayerId()
			if GetPlayerWantedLevel(playerId) ~= 0 then
				SetPlayerWantedLevel(playerId, 0, false)
				SetPlayerWantedLevelNow(playerId, false)
			end
		end
		firstLoop = nil
	end
end)

Then you need to get the online cops, so a serverside function put into policejob would do that:

/esx_policejob/server/main.lua

ESX.RegisterServerCallback('esx_policejob:getCopsOnline', function(source, cb)

	local xPlayers = ESX.GetPlayers()

	local copsOnline = 0
	for i=1, #xPlayers, 1 do
		local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
		if xPlayer.job.name == 'police' then
			copsOnline = copsOnline + 1
		end
	end

	cb(copsOnline)

end)

Now that you have the ability to check how many are online, you can check on a timer to make sure that the correct count of police is being used for the check:
/esx_policejob/client/main.lua

-- Get online cop count
Citizen.CreateThread(function()
	while true do
		Citizen.Wait(10000)
		ESX.TriggerServerCallback('esx_policejob:getCopsOnline', function(copsOnline)
			copCount = copsOnline
		end)
	end
end)

And your check line for the wanted level:

-- Disable wanted level
firstLoop = 'yes'
Citizen.CreateThread(function()
	while true do
		if firstLoop then
			Citizen.Wait(10000)
		else
			Citizen.Wait(0)
		end
		if copCount >= 1 or PlayerData.job.name == 'police' then
			local playerId = PlayerId()
			if GetPlayerWantedLevel(playerId) ~= 0 then
				SetPlayerWantedLevel(playerId, 0, false)
				SetPlayerWantedLevelNow(playerId, false)
			end
		end
		firstLoop = nil
	end
end)

No config options necessary.

Thank you again I started the esx_policejob with the changes made on it and I have an error on this line in the console


if copCount >= 1 or PlayerData.job.name == 'police' then

Were you planning on sharing what the error was?

Yes of course, after being in service and firing I got a research star

Add this to the top of your client file to populate the variable on script startup:

local copCount					= 0

I’m really bad at coding and I didn’t understand where do you want me to put the
``local copCount = 0`

local copCount = 0 goes at the top of police jobs client main.lua.

Ok ! Same error

Rather than spend the next day having me troubleshoot your changes, why don’t we give you a chance? Go to the line number of the file in question and work out what’s nil and why that is?

Yes I would really like because there I am struggling xD

Your first step is going to the line number in the error, and looking for something comparing a number to what’s called nil(meaning, a non-existent variable). So for instance:

if theNumberOfDucksInMyPocket == 2 then

Tells lua that it needs to compare the variable named "theNumberOfDucksInMyPocket to the number 2 to see if it’s equal. The problem is that we’ve never set that variable, so the script will try to compare a nil value to 2, which is impossible. To solve this, something has to happen prior to this check to set the variable. It could be something as simple as this:

theNumberOfDucksInMyPocket = 1

Now the script would compare that variable to the number 2 and return false because the two don’t match.

Your script is trying to compare a nil value, the line number is where it’s happening so look at that line and figure out what variable isn’t set. You can’t move on to a next step until you know which variable you’re having a problem with.

1 Like

what a teacher!
I remember when i was like this guy over here in ur esx help discord, and u were answering my dumb questions haha

1 Like

Hi man, I’m probably boring but I couldn’t find anything else

ok mb i found the probleme thanks a lot

some beautiful person needs to bundle this up in their esx_policejob and share it, or somehow make it stand alone. Reading through all the comments made my eyes bleed. :slight_smile:

2 Likes