[Help] Optimize this function

Hello,

I ask for help to optimize this function, if you have any idea to me proposed please tell me !!!

This permer function finds the player who is close to the player who is calling this function

function GetPlayerBeside(Players, distanceMin)
  local PlayerPositions = {}
  
  for i=1,#Players do
      local Player = Players[i]
      local PlayerFromServerId = GetPlayerFromServerId(Player.Source)
      local PlayerPed = GetPlayerPed(PlayerFromServerId)
      local NamePlayer = GetPlayerName(PlayerFromServerId)
      if GetPlayerPed(-1) ~= PlayerPed then
        local x, y, z = table.unpack(GetEntityCoords(PlayerPed, true))
        PlayerPositions[#PlayerPositions+1] = {Source=Player.Source, Postion={x=x, y=y, z=z}, NamePlayer=NamePlayer }
      end
  end

  local PlayerBeside = {Distance=distanceMin, Source=0, NamePlayer="$£INCONNE$" }

  for i=1,#PlayerPositions do
     local PlayerPosition = PlayerPositions[i].Postion
     local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
     local Distance = Vdist(x, y, z, PlayerPosition.x, PlayerPosition.y, PlayerPosition.z)
     if  Distance <= distanceMin then
         if Distance <= PlayerBeside.Distance then
            PlayerBeside.Distance = Distance
            PlayerBeside.Source = PlayerPositions[i].Source
            PlayerBeside.NamePlayer = PlayerPositions[i].NamePlayer
         end
     end
  end
  
  Citizen.Trace("Debug : " .. PlayerBeside.Source .. " " .. PlayerBeside.NamePlayer .. " Distance :" .. PlayerBeside.Distance)
  return PlayerBeside
end
function GetPlayers()
	local players = {}

	for i = 0, 31 do
		if NetworkIsPlayerActive(i) then
			table.insert(players, i)
		end
	end

	return players
end

function GetClosestPlayer()
	local players = GetPlayers()
	local closestDistance = -1
	local closestPlayer = -1
	local ply = GetPlayerPed(-1)
	local plyCoords = GetEntityCoords(ply, 0)
	
	for index,value in ipairs(players) do
		local target = GetPlayerPed(value)
		if(target ~= ply) then
			local targetCoords = GetEntityCoords(GetPlayerPed(value), 0)
			local distance = GetDistanceBetweenCoords(targetCoords["x"], targetCoords["y"], targetCoords["z"], plyCoords["x"], plyCoords["y"], plyCoords["z"], true)
			if(closestDistance == -1 or closestDistance > distance) then
				closestPlayer = value
				closestDistance = distance
			end
		end
	end
	
	return closestPlayer, closestDistance
end

Not from me

Ty : I will test this ^^