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 :: random number lua 
Lua :: int and float in lua 
Lua :: how to make auto scroll roblox 
Lua :: 1.2 / 1.6 
Lua :: insert item array pico8 
Lua :: lua code 
Lua :: check if play is in group 
Lua :: roblox studio buying robux 
Lua :: lua table of alphabet 
Matlab :: to detect if a data frame has nan values 
Matlab :: octave disable warning 
Matlab :: matlab 
Matlab :: cumprod matlab 
Matlab :: cos in scilab 
Basic :: bash catch ctrl-c in a script 
Basic :: add firefox 
Elixir :: elixir string to time 
Elixir :: elixir timex format 
Elixir :: elixir enum chunk_by 
Scala :: scala string to boolean 
Scala :: scala list all permutations 
Actionscript :: mount_osxfuse : Input/output error 
Excel :: convert number to date in excel 
Perl :: perl hash ref create 
Pascal :: pascal wait for key 
Powershell :: takeown /f 
Clojure :: hello world in clojure 
Assembly :: restart kde 
Assembly :: Z language latex 
Javascript :: jquery vslidation remove spaces from input 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =