[Help] Optimize script to disable dispatch, ai cops and npc weapon drop

Hello,
i have a small script, that does the following:

  • Disable weapon drops from NPC

  • Disable EMS dispatch

  • Disable all ai cops

Problem is, when i run resmon 1 in console, this script takes alot of resources and drops fps.
Would there be any way to optimise this script? Thanks!

local pedindex = {}

function SetWeaponDrops() 
    local handle, ped = FindFirstPed()
    local finished = false 
    repeat 
        if not IsEntityDead(ped) then
                pedindex[ped] = {}
        end
        finished, ped = FindNextPed(handle)
    until not finished
    EndFindPed(handle)

    for peds,_ in pairs(pedindex) do
        if peds ~= nil then
            SetPedDropsWeaponsWhenDead(peds, false) 
        end
    end
end

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
		SetWeaponDrops()

		for i = 1, 15 do
			EnableDispatchService(i, false)
		end

		local playerPed = GetPlayerPed(-1)
		local playerLocalisation = GetEntityCoords(playerPed)
		ClearAreaOfCops(playerLocalisation.x, playerLocalisation.y, playerLocalisation.z, 400.0)
	end
end)

See: [Best practice] Improve your resource performance

Citizen.Wait(0) means you are running this code every tick. Increase the number in ms. Citizen.Wait(100)

You don’t need to call EnableDispatchService every frame, one time is enough.
Also executing RemoveAllPickupsOfType(14) every frame is better than your ped loop.

ClearAreaOfCops every tick is also expensive. Try SetMaxWantedLevel(0) and this only needs to be set once. I think this is a more desired behavior as cops will keep coming even if you “delete” them

EDIT: Didn’t notice you wanted to turn off all Dispatch AI. Just use your EnableDispatcheService but call it only once as previously suggested. Dont need ClearAreaOfCops

This is old, but the reason you want ClearAreaOfCops is because it deletes police vehicles instead of just disabling the dispatching.