For example using a function like this:
closestTarget(1)
Will return the ID of the player closest to the player with the ID of 1.
Thanks in advance.
Scripts
How to get the closest player ID?
How to get the closest player ID?
1

closestTarget(1)
function distance(x1, y1, x2, y2)
return (math.sqrt((x2-x1)^2 + (y2-y1)^2))
end
function closestTarget(id)
local closest;
for _, target in ipairs(player(0,"tableliving")) do
if id ~= target then
local d = distance(player(id, "x"), player(id, "y"), player(target, "x"), player(target, "y"))
if closest == nil or d < closest.dist then
closest = {id = target, dist = d}
end
end
end
return closest ~= nil and closest.id or nil
end
1
