I need a little bit of help with my script (ESX & OX) (Optimization)

Hello everyone.
I’m using an un-optimized script at this time and I try to optimize it.
It’s currenlty using 5.30 ms in-game when nobody is using the resource.

I’d like to add ox-target to it, instead of ESX.Game.Utils.DrawText3D.
I tried to add an ox-target to call the function OpenTrashCan but this just didn’t want to work.

Can someone help me on the way with this?
I really don’t know if that will optimize it or not.
If someone else have more advises/tips to optimize it back to 0.00-0.15 ms, let me know <3

client.lua

ESX                           = nil

local cachedBins = {}

Citizen.CreateThread(function ()
    while ESX == nil do
        TriggerEvent('esx:getSharedObject', function(obj) 
            ESX = obj 
        end)
        Citizen.Wait(5)
    end
end)

Citizen.CreateThread(function()
    Citizen.Wait(1000)

    for locationIndex = 1, #Config.SellBottleLocations do
        local locationPos = Config.SellBottleLocations[locationIndex]
        local blip = AddBlipForCoord(locationPos)
        SetBlipSprite (blip, 651)
        SetBlipDisplay(blip, 4)
        SetBlipScale  (blip, 0.8)
        SetBlipColour (blip, 48)
        SetBlipAsShortRange(blip, true)
        BeginTextCommandSetBlipName("STRING")
        AddTextComponentString("Inleverpunt Statiegeld")
        EndTextCommandSetBlipName(blip)
    end

    while true do
        local sleepThread = 500
        local ped = PlayerPedId()
        local pedCoords = GetEntityCoords(ped)

        for locationIndex = 1, #Config.SellBottleLocations do
            local locationPos = Config.SellBottleLocations[locationIndex]
            local dstCheck = GetDistanceBetweenCoords(pedCoords, locationPos, true)

            if dstCheck <= 5.0 then
                sleepThread = 5

                if dstCheck <= 1.5 then
                    local text = "[~g~E~s~] Statiegeld inleveren"
                    ESX.Game.Utils.DrawText3D(locationPos, text, 0.4)
                    if IsControlJustReleased(0, 38) then
                        TriggerServerEvent("zh_statiegeld:sellBottles")
                    end
                end
            end
        end

        Citizen.Wait(sleepThread)
    end
end)

Citizen.CreateThread(function()
    Citizen.Wait(100)

    while true do
        local sleepThread = 1000
        local playerPed = PlayerPedId()
        local playerCoords = GetEntityCoords(playerPed)
        local closestObject, closestDst = nil, nil

        for _, objectName in ipairs(Config.BinsAvailable) do
            local object = GetClosestObjectOfType(playerCoords, 2.0, GetHashKey(objectName), false, false, false)
            if object ~= nil then
                local dst = #(playerCoords - GetEntityCoords(object))
                if closestDst == nil or dst < closestDst then
                    closestObject = object
                    closestDst = dst
                end
            end
        end
        
        if closestObject ~= nil then
            sleepThread = 5
            local binCoords = GetEntityCoords(closestObject)
            local text = "[~g~E~s~] Zoek in prullenbak"

            if closestDst <= 1.7 then
                ESX.Game.Utils.DrawText3D(binCoords + vector3(0.0, 0.0, 0.5), text, 0.4)
                if IsControlJustReleased(0, 38) then
                    if not cachedBins[closestObject] then
                        cachedBins[closestObject] = true
                        OpenTrashCan(closestObject)
                    else
                        ESX.ShowNotification("Deze prullenbak is al doorzocht!")
                    end
                end
            end
        end

        Citizen.Wait(sleepThread)
    end
end)


function OpenTrashCan(entity)
    local playerPed = PlayerPedId()
    local binCoords = GetEntityCoords(entity)
    local playerCoords = GetEntityCoords(playerPed)
    local heading = GetHeadingFromVector_2d(binCoords.x - playerCoords.x, binCoords.y - playerCoords.y)

    TaskTurnPedToFaceCoord(playerPed, binCoords.x, binCoords.y, binCoords.z, -1)
    Citizen.Wait(1000)
    TaskStartScenarioInPlace(playerPed, "PROP_HUMAN_BUM_BIN", 0, true)
    Citizen.Wait(6000)
    TriggerServerEvent("zh_statiegeld:retrieveBottle")
    ClearPedTasks(playerPed)
end

Fixed it myself eventually.
Thanks for the great help here…!

:lock: CLOSED :lock: