Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR LUA

fivem get closest player

-- 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()
 
PREVIOUS NEXT
Tagged: #fivem #closest #player
ADD COMMENT
Topic
Name
6+3 =