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 :: pico8 draw sprite 
Lua :: How to make a working gun in lua code 
Lua :: insert item array pico8 
Lua :: convert a float to string lua 
Lua :: C++ 914 card 
Lua :: genarating random number 
Lua :: lua catch error 
Lua :: Simple Roblox Lua Function 
Matlab :: matlab how to set figure size so you can see plot 
Matlab :: matlab if not true 
Matlab :: matlab preallocate array size 
Matlab :: print hello world n times in matlab 
Matlab :: how to set for with 2 increases in matlab 
Basic :: cmd cant change directory 
Basic :: JWT EM VBNET 
Basic :: vb.net printing set page size 
Elixir :: elixir enum map 
Elixir :: elixir alias __module__ 
Scala :: scala concatenate list 
Scala :: Category Theory laws in scala 
Actionscript :: reset udemy course 
Excel :: google sheets select element after split 
Excel :: Date and Time maths with Excel 
Perl :: difference between @ and $ in perl 
Pascal :: pascal repetition 
Powershell :: powershell show which diorecty is temp 
Abap :: abap shortcut comments 
Assembly :: ror loading webview: Error: Could not register service workers: InvalidStateError: Failed to register a ServiceWorker: The document is in an invalid state.. 
Assembly :: datauristring pdf open in php 
Javascript :: jquery vslidation remove spaces from input 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =