Back to Library
Utility Nov 23, 2025

Closest Player

T
by terrorsoul

Loops through all players and returns the ID of the one closest to a specific position, excluding the caller.

LUA
function GetClosestPlayer(originPos, ignoreId)
    local players = tm.players.CurrentPlayers()
    local closestId = -1
    local closestDist = 999999
    
    for _, p in pairs(players) do
        if p.playerId ~= ignoreId then
            local targetPos = tm.players.GetPlayerTransform(p.playerId).GetPosition()
            local dist = tm.vector3.Distance(originPos, targetPos)
            
            if dist < closestDist then
                closestDist = dist
                closestId = p.playerId
            end
        end
    end
    
    return closestId
end

Discussion

Please log in to post a comment.