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
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)
It then triggers the serverevent:
-- 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)
And then goed back to the client event:
-- 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 <= 1.2 then
-- Trigger tag event
TriggerServerEvent("cartag:tag", closestPlayerId)
end
end
end
end)
The GetClosestPlayer looks like this:
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
This also calls upon:
local players = {}
for _, player in ipairs(GetActivePlayers()) do
local ped_ = GetPlayerPed(player)
table.insert(players, ped_)
end
return players
end
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.
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) )