I’m trying to make players loot materials from dump cars that can be found in several places in the map
but There is a line that causes the resource to use more than 0.30ms and I was trying to optimize it but couldn’t figure out a better way
the line:
local car = GetClosestObjectOfType(pos.x, pos.y, pos.z, 3.0, Car[i], false, false, false)
this thing is always searching objects nearby and try to match them to the dump cars hashes in order to start the loot thread
here’s the part of the code that need optimization
local searched = {3423423424}
local canSearch = true
local Car = {10106915, 322493792, -273279397, -915224107, 591265130, 1120812170}
local searchTime = 14000
Citizen.CreateThread(function(time)
while true do
Citizen.Wait(5)
if canSearch then
local ped = GetPlayerPed(-1)
local pos = GetEntityCoords(ped)
local carFound = false
for i = 1, #Car do
local car = GetClosestObjectOfType(pos.x, pos.y, pos.z, 3.0, Car[i], false, false, false)
local dumpPos = GetEntityCoords(car)
local dist = GetDistanceBetweenCoords(pos.x, pos.y, pos.z, dumpPos.x, dumpPos.y, dumpPos.z, true)
local playerPed = PlayerPedId()
if dist < 3 then
DrawText3Ds(dumpPos.x, dumpPos.y, dumpPos.z + 1.0, 'Appuyez [~o~E~w~] pour chercher ~o~la voiture')
if IsControlJustReleased(0, 54) then
for i = 1, #searched do
if searched[i] == car then
carFound = true
end
if i == #searched and carFound then
exports['mythic_notify']:SendAlert('error', 'Cette voiture a déjà été recherchée')
elseif i == #searched and not carFound then
exports['mythic_notify']:SendAlert('inform', 'Vous commencez à fouiller la voiture')
startSearching(searchTime, 'amb@prop_human_bum_bin@base', 'base', 'sbop:server:rewarditem')
TriggerServerEvent('carscrap:startcarTimer', car)
table.insert(searched, car)
Citizen.Wait(1000)
end
end
end
end
end
end
end
end)