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 server id of nearest player

-- 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
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 :: lua wiki 
Lua :: Lua how to comment 
Matlab :: matlab unix time to datetime 
Matlab :: matlab inverse z transform 
Matlab :: find duplicates in matlab arrauy 
Matlab :: sum vs symsum in matlab script 
Matlab :: matlab pause code run while simulink finishes 
Matlab :: octave wait 
Matlab :: matlab find roots of symbolic polynomial 
Matlab :: octave get range length 
Basic :: cmd cd not working 
Basic :: basic authentication bash 
Basic :: wussup 
Elixir :: elixir length of list 
Elixir :: phoenix run server 
Elixir :: elixir string interpolation 
Scala :: scala random number 
Scala :: find a list of strings inside string scala 
Scala :: scala check if seq container true booleans 
Excel :: excel use offset in conditional formatting 
Excel :: AND logic in excel formula 
Perl :: unique in perl 
Pascal :: pascal output 
Powershell :: Take ownership of a file 
Gdscript :: GDScript typed variables 
Cobol :: cobol 
Assembly :: multiply two numbers assembly lmc 
Assembly :: why assembly language use register 
Javascript :: jquery vslidation remove spaces from input 
Javascript :: jquery unselect option 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =