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