I’m trying to optimize this script [ESX] Onyx Dumpster Diving - #63 by happylowmandk and most of the optimization goes through GetClosestObjectOfType which I think gets all the coordinates of the map objects and then choose the closest one and my idea was to do the same but only with objects that are close to the player
ESX = nil
local searched = {3423423424}
local canSearch = true
local dumpsters = {218085040, 666561306, -58485588, -206690185, 1511880420, 682791951}
local searchTime = 14000
local dumpPos = {}
Citizen.CreateThread(function()
while ESX == nil do
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
Citizen.Wait(0)
end
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
if canSearch then
local ped = GetPlayerPed(-1)
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)
local dumpPos = GetEntityCoords(dumpster)
local dist = GetDistanceBetweenCoords(pos.x, pos.y, pos.z, dumpPos.x, dumpPos.y, dumpPos.z, true)
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
for i = 1, #searched do
if searched[i] == dumpster then
dumpsterFound = true
end
if i == #searched and dumpsterFound then
exports['mythic_notify']:DoHudText('error', 'This dumpster has already been searched')
elseif i == #searched and not dumpsterFound then
exports['mythic_notify']:DoHudText('inform', 'You begin to search the dumpster')
startSearching(searchTime, 'amb@prop_human_bum_bin@base', 'base', 'onyx:giveDumpsterReward')
TriggerServerEvent('onyx:startDumpsterTimer', dumpster)
table.insert(searched, dumpster)
end
end
end
end
end
end
end
end)
RegisterNetEvent('onyx:removeDumpster')
AddEventHandler('onyx:removeDumpster', function(object)
for i = 1, #searched do
if searched[i] == object then
table.remove(searched, i)
end
end
end)
-- Functions
function startSearching(time, dict, anim, cb)
local animDict = dict
local animation = anim
local ped = GetPlayerPed(-1)
canSearch = false
RequestAnimDict(animDict)
while not HasAnimDictLoaded(animDict) do
Citizen.Wait(0)
end
exports['progressBars']:startUI(time, "Searching Dumpster")
TaskPlayAnim(ped, animDict, animation, 8.0, 8.0, time, 1, 1, 0, 0, 0)
local ped = GetPlayerPed(-1)
Wait(time)
ClearPedTasks(ped)
canSearch = true
TriggerServerEvent(cb)
end
function DrawText3Ds(x, y, z, text)
local onScreen,_x,_y=World3dToScreen2d(x,y,z)
local scale = 0.5
if onScreen then
SetTextScale(scale, scale)
SetTextFont(6)
SetTextProportional(1)
SetTextColour(255, 255, 255, 215)
SetTextOutline()
SetTextEntry("STRING")
SetTextCentre(1)
AddTextComponentString(text)
DrawText(_x,_y)
end
end