Search
 
SCRIPT & CODE EXAMPLE
 

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()
Comment

FiveM how to check where nearest player is

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
Comment

PREVIOUS NEXT
Code Example
Lua :: genarating random number 
Lua :: lua table concanation 
Lua :: roblox studio buying robux 
Lua :: global variables lua 
Lua :: Simple Roblox Lua Function 
Matlab :: read all files from folder matlab 
Matlab :: matlab measure time 
Matlab :: matlab delete file 
Matlab :: matlab root finding function 
Matlab :: how to run a p code matlab 
Matlab :: what is java_home 
Matlab :: matlab invert image 
Basic :: pmatplotlib draw a square with a magenta dotted line and pentagon markersython matplotlib overlaped 
Basic :: dos assign command output to variable (when output is a single line) 
Elixir :: elixir after 
Elixir :: phoenix system get env 
Elixir :: elixir anonymous function 
Scala :: scala random number 
Scala :: dataframe column json parser spark scala 
Actionscript :: move records from table to another using knex migration 
Excel :: google sheets select element after split 
Excel :: how to lock row in excel 
Perl :: fonction perl 
Pascal :: pascal delay 
Powershell :: How to test HDD health in PowerShell 
Clojure :: toggle button clojure 
Assembly :: openssl public key der to pem 
Assembly :: visual studio change assembly name 
Javascript :: jquery vslidation remove spaces from input 
Javascript :: jquery unselect option 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =