Letting the thread sleep if there’s no dumpsters around can help:

Citizen.CreateThread(function()
    while true do
        local letSleep = true
        
        if canSearch then
            local ped = PlayerPedId()
            local pos = GetEntityCoords(ped)
            local dumpsterFound = false

            for i = 1, #dumpsters do
                local dumpster = GetClosestObjectOfType(pos.x, pos.y, pos.z, 1.0, dumpsters[i], false, false, false)

                if dumpster then
                    letSleep = false -- dumpster found so prevent thread from sleeping
                    local dumpPos = GetEntityCoords(dumpster)
                    local dist = #(dumpPos - pos)

                    if dist < 1.8 then
                        DrawText3Ds(dumpPos.x, dumpPos.y, dumpPos.z + 1.0, '[~g~H~w~] Procurar no lixo')

                        if IsControlJustReleased(0, 74) then
                            --
                        end
                    end
                end
            end
        end

        -- No dumpsters in range so let the thread sleep
        if letSleep then
            Citizen.Wait(1000)
        end

        Citizen.Wait(0)
    end
end)
1 Like