Hello amirmahdi,
this is done in two simple steps. (serverside)
First we calculate the nearest vehicle of the player. This can be done by getting all vehicles on the server and using the distance metric to find the vehicle with the smallest distance and return it.
local function findClosestVehicle(elem)
local vehicles = getElementsByType("vehicle");
local elem_x, elem_y, elem_z = getElementPosition(elem);
local closest_veh = false;
local closest_dist = false;
for m,veh in ipairs(vehicles) do
local veh_x, veh_y, veh_z = getElementPosition(veh);
local dist = getDistanceBetweenPoints3D(elem_x, elem_y, elem_z, veh_x, veh_y, veh_z);
if not (closest_dist) or (closest_dist > dist) then
closest_dist = dist;
closest_veh = veh;
end
end
return closest_veh;
end
Next we create a command handler "rscar" which uses our new function to respawn the closest vehicle.
addCommandHandler("rscar",
function(player)
local veh = findClosestVehicle(player);
if not (veh) then
outputChatBox("could not find close vehicle", player);
return;
end
-- TODO: use respawnVehicle to place the vehicle on the map again.
end
);
Good luck!