[Help] I need help dividing esx_selldrugs between City and Paleto & Sandy locations based on Police/Sheriffs on duty

Hi I just started learning LUA, and I’m stuck on one thing. Could you guys help?

I’m trying to divide selling drugs script to two sections based on Police and Sheriffs on duty and the location. Maybe someone already has script like this one? If not I’ll gladly share it when completed.

So if enough Cops on duty selling location in the City, and if enough Sheriffs on duty selling location Sandy & Paleto. Mostly it is working fine for me but for some reason I’m getting that line triggered all the time when next to the Ped and can’t figure out why that’s happening.

ESX.ShowNotification(_U('too_far_away'))

I’ll really appreciate if someone could help me.That’s the rest of the code from /client/main.lua

...
currentped = nil
Citizen.CreateThread(function()
	while true do
		Wait(10)
		local player = GetPlayerPed(-1)
		local pid = PlayerPedId()
  		local playerloc = GetEntityCoords(player, 0)
		local handle, ped = FindFirstPed()
		local success
		repeat
		    success, ped = FindNextPed(handle)
		   	local pos = GetEntityCoords(ped)
			local distance = GetDistanceBetweenCoords(pos.x, pos.y, pos.z, playerloc['x'], playerloc['y'], playerloc['z'], true)
			
			-- Distance from the City
			local distanceFromCity = GetDistanceBetweenCoords(Config.CityPoint.x, Config.CityPoint.y, Config.CityPoint.z, playerloc['x'], playerloc['y'], playerloc['z'], true)
			-- Distance from Paleto
			local distanceFromPaleto = GetDistanceBetweenCoords(Config.PaletoPoint.x, Config.PaletoPoint.y, Config.PaletoPoint.z, playerloc['x'], playerloc['y'], playerloc['z'], true)
			-- Distance from Sandy
			local distanceFromSandy = GetDistanceBetweenCoords(Config.SandyPoint.x, Config.SandyPoint.y, Config.SandyPoint.z, playerloc['x'], playerloc['y'], playerloc['z'], true)

			if IsPedInAnyVehicle(GetPlayerPed(-1)) == false then
				if DoesEntityExist(ped)then
					if IsPedDeadOrDying(ped) == false then
						if IsPedInAnyVehicle(ped) == false then
							local pedType = GetPedType(ped)
							if pedType ~= 28 and IsPedAPlayer(ped) == false then
								currentped = pos
								if distance <= 2 and ped  ~= GetPlayerPed(-1) and ped ~= oldped and IsControlJustPressed(1, 38) and not timeout then
									timeout = true
									SetTimeout()
									TriggerServerEvent('check')
									RegisterNetEvent('countOnDuty')
									AddEventHandler('countOnDuty', function(cops, sheriffs)
										if distanceFromCity < Config.DistanceFromCity then
											if cops >= Config.CopsRequiredToSell then
												print('Cops reuired to sell: ' .. Config.CopsRequiredToSell)
												print('Cops on Duty: ' .. cops)
												ESX.ShowNotification('SELLING DRUGS CITY')
												if playerHasDrugs and sold == false and selling == false then 
													--PED REJECT OFFER
													local random = math.random(1, Config.PedRejectPercent)
													if random == Config.PedRejectPercent then
														ESX.ShowNotification(_U('reject'))
														oldped = ped
														--PED CALLING COPS
														if Config.CallCops then
															local randomReport = math.random(1, Config.CallCopsPercent)
															print(Config.CallCopsPercent)
															if randomReport == Config.CallCopsPercent then
																TriggerServerEvent('drugsNotify')
															end
														end
														TriggerEvent("sold")
													--PED ACCEPT OFFER
													else
														SetEntityAsMissionEntity(ped)
														ClearPedTasks(ped)
														FreezeEntityPosition(ped,true)
														oldped = ped										
														TaskStandStill(ped, 9)
														pos1 = GetEntityCoords(ped)
														TriggerEvent("sellingdrugs")
													end
												end
											else
												ESX.ShowNotification(_U('must_be') .. Config.CopsRequiredToSell .. _U('to_sell_drugs_cops'))
											end
										elseif distanceFromPaleto < Config.DistanceFromPaleto or distanceFromSandy < Config.DistanceFromSandy then
											if sheriffs >= Config.SherRequiredToSell then
												print('Sheriffs reuired to sell: ' .. Config.SherRequiredToSell)
												print('Sheriffs on Duty: ' .. sheriffs)
												ESX.ShowNotification('SELLING DRUGS PALETO i SANDY')
												if playerHasDrugs and sold == false and selling == false then 
													--PED REJECT OFFER
													local random = math.random(1, Config.PedRejectPercent)
													if random == Config.PedRejectPercent then
														ESX.ShowNotification(_U('reject'))
														oldped = ped
														--PED CALLING COPS
														if Config.CallCops then
															local randomReport = math.random(1, Config.CallCopsPercent)
															print(Config.CallCopsPercent)
															if randomReport == Config.CallCopsPercent then
																TriggerServerEvent('drugsNotify')
															end
														end
														TriggerEvent("sold")
													--PED ACCEPT OFFER
													else
														SetEntityAsMissionEntity(ped)
														ClearPedTasks(ped)
														FreezeEntityPosition(ped,true)
														oldped = ped										
														TaskStandStill(ped, 9)
														pos1 = GetEntityCoords(ped)
														TriggerEvent("sellingdrugs")
													end
												end
											else
												ESX.ShowNotification(_U('must_be') .. Config.SherRequiredToSell .. _U('to_sell_drugs_sher'))
											end
										else
											ESX.ShowNotification(_U('too_far_away_from_city'))
										end
									end)
								end
							end
						end
					end
				end
			end
		until not success

		EndFindPed(handle)
	end	
end)

function SetTimeout()
	Citizen.CreateThread(function()
		while timeout do
			Citizen.Wait(5000)

			timeout = false
		end
	end)
end

Citizen.CreateThread(function()
	while true do
		Wait(100)
		if selling then
			local player = GetPlayerPed(-1)
  			local playerloc = GetEntityCoords(player, 0)
			local distance = GetDistanceBetweenCoords(pos1.x, pos1.y, pos1.z, playerloc['x'], playerloc['y'], playerloc['z'], true)
			local pid = PlayerPedId()
			--TOO FAR
			if distance > 5 then
				ESX.ShowNotification(_U('too_far_away'))
				selling = false
				SetEntityAsMissionEntity(oldped)
				SetPedAsNoLongerNeeded(oldped)
				FreezeEntityPosition(oldped,false)
			end
			--SUCCESS
			if secondsRemaining <= 1 then
				selling = false
				SetEntityAsMissionEntity(oldped)
				SetPedAsNoLongerNeeded(oldped)
				FreezeEntityPosition(oldped,false)
				StopAnimTask(pid, "amb@prop_human_bum_bin@idle_b","idle_d", 1.0)
				playerHasDrugs = false
				sold = false
				TriggerServerEvent('sellDrugs')
			end	
			
			if secondsRemaining == 3 and Config.PlayAnimation then
				Citizen.Wait(1100) 
				RequestAnimDict("amb@prop_human_bum_bin@idle_b")
				while (not HasAnimDictLoaded("amb@prop_human_bum_bin@idle_b")) do 
					Citizen.Wait(0) 
				end
				ClearPedTasksImmediately(pid)
				TaskPlayAnim(pid,"amb@prop_human_bum_bin@idle_b","idle_d",100.0, 200.0, 0.3, 120, 0.2, 0, 0, 0)
			end
		end	
	end
end)	
...