A scrap looting script using too much resource

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)

Sleep it.

Above the while true, add local sleep = 5000.
Below the while true, replace Citizen.Wait(5) with Wait(sleep).
Below the Wait(sleep) add Sleep = 5000
Below the if dist < 3 then add sleep = 0.

No reason to check every frame unless the player needs to do it while doing a timed task such as driving, running, or being chased.

I thought of that but the 3D Text start disappearing and then reappearing every 5000 ms.
Any way of fixing that?

1 Like

It’s all where you place the code my man. I’m fairly certain I told you the right places for it to prevent the text flickering. If not, I can try again using your block of code :slight_smile:

I’ll try your method right now and reply back very soon

also why are you defining sleep two times?

I think you want the script to change the sleep duration to match the 3D text and so.

This one should be sleep not Sleep right?
it made me confused

You’re right! The capital ‘S’ is supposed to be lowercase. Apologies!

Huge thanks dude
image
but people must stand next to the car for 5 seconds before the text draw happens right?
I also noticed that it’s getting to ~0.70ms while standing next to the dump car
is it normal or am I doing something else wrong

People do have to stand next to the car for 5 seconds, yes.

Honestly, you’re not doing anything wrong, most scripts just aren’t built in an optimized way. You could certainly optimize away that delay before it draws and the 0.7! But it would require you to restructure that entire block you posted, and even then plus some.

Sadly, it’s out of the scope of my possibility to do all that for you due to my break ending :frowning:

1 Like

It’s still amazing that you helped me solve my main problem!
What remains is my responsibility.

1 Like

are you planing to share this script?