title: How to get closest player (FiveM Lua)
body: <p>This is a question regarding FiveM Scripting using the LUA language;</p>
  
  <p>I want to get the ID or name of the closest player to the player executing the event. How do I get this? I currently have: (Client.lua)</p>
  
  <pre><code>    while true do
          Citizen.Wait(0)
          local ped = GetPlayerPed(-1)
          if IsPedInAnyVehicle(ped, false) then
              local vehicle = GetVehiclePedIsIn(ped, false)
              TriggerServerEvent("cartag:checkTagged")
          end
      end
  end)
  </code></pre>
  
  <p>It then triggers the server event:</p>
  
  <pre><code>-- Checks if player is tagged
  AddEventHandler("cartag:checkTagged", function()
      local ped = source
      if tableIndex(tag, ped) then
          local indexNr = tableIndex(tag, ped)
          if tag[indexNr].tagged then
              TriggerClientEvent("cartag:playerIt", ped)
          end
      end
  end)
  </code></pre>
  
  <p>And then goes back to the client event:</p>
  
  <pre><code>-- When player is it
  AddEventHandler("cartag:playerIt", function()
      local ped = GetPlayerPed(-1)
      local vehicle = GetVehiclePedIsIn(ped, false)
      local t, distance = GetClosestPlayer()
      if IsPedInAnyVehicle(t, false) then
          if HasEntityCollidedWithAnything(vehicle) then
              local dis = Vdist(coords.x, coords.y, coords.z, coordsFront.x, coordsFront.y, coordsFront.z)
              if dis &lt;= 1.2 then
                  -- Trigger tag event
                  TriggerServerEvent("cartag:tag", closestPlayerId)
              end
          end
      end
  end)
  </code></pre>
  
  <p>The GetClosestPlayer looks like this:</p>
  
  <pre><code>    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 &gt; distance) then
                  closestPlayer = v
                  closestDistance = distance
              end
          end
      end
      return closestPlayer, closestDistance
  end
  </code></pre>
  
  <p>This also calls upon:</p>
  
  <pre><code>    local players = {}
      for _, player in ipairs(GetActivePlayers()) do
          local ped_ = GetPlayerPed(player)
          table.insert(players, ped_)
      end
      return players
  end
  </code></pre>
  
  <p>Somehow this always gives a random number when testing, with the distance pointing towards a spawn point under the map, in the northern part of the city.</p>
  
  <p>So; How do I check who the closest player is, how far this player is and if that player is in a car?
  Thanks in advance! 
  (Tried things from other topics, but they didn't seem to work)
  Note: I don't want to use ESX, want to make the script stand alone. (It's a tag game script for cars... sounds weird, don't blame me x) )</p>