-- with esx
local player, distance = ESX.Game.GetClosestPlayer()
-- without esx
function GetClosestPlayer()
local players = GetPlayers()
local closestDistance = -1
local closestPlayer = -1
local ped = GetPlayerPed(-1)
local coords = GetEntityCoords(ped)
for i, v in ipairs(players) do
local target = GetPlayerPed(v)
if (target ~= ped) then
local targetCoords = GetEntityCoords(GetPlayerPed(v))
local distance = Vdist(targetCoords.x, targetCoords.y, targetCoords.z, coords.x, coords.y, coords.z)
if (closestDistance == -1 or closestDistance > distance) then
closestPlayer = v
closestDistance = distance
end
end
end
return closestPlayer, closestDistance
end
local player, distance = GetClosestPlayer()
-- with esx
local player, distance = ESX.Game.GetClosestPlayer()
if distance <= 5.0 --[[the maximum distance to the other player]] then
local player_server_id = GetPlayerServerId(player)
end
-- without esx
function GetClosestPlayer()
local players = GetPlayers()
local closestDistance = -1
local closestPlayer = -1
local ped = GetPlayerPed(-1)
local coords = GetEntityCoords(ped)
for i, v in ipairs(players) do
local target = GetPlayerPed(v)
if (target ~= ped) then
local targetCoords = GetEntityCoords(GetPlayerPed(v))
local distance = Vdist(targetCoords.x, targetCoords.y, targetCoords.z, coords.x, coords.y, coords.z)
if (closestDistance == -1 or closestDistance > distance) then
closestPlayer = v
closestDistance = distance
end
end
end
return closestPlayer, closestDistance
end
local player, distance = GetClosestPlayer()
if distance <= 5.0 --[[the maximum distance to the other player]] then
local player_server_id = GetPlayerServerId(player)
end
For example, you could use GET_CLOSEST_PLAYER_TO_ENITY then, check to make sure that the player is within a certain distance. There, you’ve just reduced the search from 32 players to one.
If you want to be able to cuff any ped then you could use GET_CLOSEST_PED