<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=EnternalEnvy</id>
	<title>Multi Theft Auto: Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=EnternalEnvy"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/wiki/Special:Contributions/EnternalEnvy"/>
	<updated>2026-05-08T19:19:23Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=GetVehiclesCountByType&amp;diff=49937</id>
		<title>GetVehiclesCountByType</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=GetVehiclesCountByType&amp;diff=49937"/>
		<updated>2016-12-19T23:08:54Z</updated>

		<summary type="html">&lt;p&gt;EnternalEnvy: /* Authors */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__NOTOC__&lt;br /&gt;
{{Useful Function}}&lt;br /&gt;
This function returns the amount of vehicles by the given type (see [[GetVehicleType]]) as an integer value.&lt;br /&gt;
&lt;br /&gt;
===Authors===&lt;br /&gt;
* '''EnternalEnvy''': Original code and idea&lt;br /&gt;
* '''Necktrox''': Code quality and performance edits&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
int getVehiclesCountByType ( string vehicleType )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Required arguments===&lt;br /&gt;
* '''vehicleType''': The vehicle type string (see list below).&lt;br /&gt;
&lt;br /&gt;
===Vehicle type list===&lt;br /&gt;
{{VehicleTypes}}&lt;br /&gt;
&lt;br /&gt;
===Return===&lt;br /&gt;
Returns an ''integer'' with the count of the vehicles with the type (returns ''0'' if the type is not supported), otherwise returns ''nil''.&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&amp;lt;section name=&amp;quot;Function source&amp;quot; class=&amp;quot;both&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function getVehiclesCountByType(vehicleType)&lt;br /&gt;
    assert(type(vehicleType) == &amp;quot;string&amp;quot;, &amp;quot;expected string at argument 1, got &amp;quot;.. type(vehicleType))&lt;br /&gt;
&lt;br /&gt;
    local getVehicleType = getVehicleType -- Localize&lt;br /&gt;
    local vehicleList = getElementsByType(&amp;quot;vehicle&amp;quot;)&lt;br /&gt;
    local vehicleCount = #vehicleList&lt;br /&gt;
    local typeCount = 0&lt;br /&gt;
&lt;br /&gt;
    for index = 1, vehicleCount do&lt;br /&gt;
        if getVehicleType(vehicleList[index]) == vehicleType then&lt;br /&gt;
            typeCount = typeCount + 1&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return typeCount&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&amp;lt;section name=&amp;quot;Server&amp;quot; class=&amp;quot;server&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
This example writes the amount of each vehicle type into the server log every minute.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local types = {&lt;br /&gt;
    &amp;quot;Automobile&amp;quot;,&lt;br /&gt;
    &amp;quot;Plane&amp;quot;,&lt;br /&gt;
    &amp;quot;Bike&amp;quot;,&lt;br /&gt;
    &amp;quot;Helicopter&amp;quot;,&lt;br /&gt;
    &amp;quot;Boat&amp;quot;,&lt;br /&gt;
    &amp;quot;Train&amp;quot;,&lt;br /&gt;
    &amp;quot;Trailer&amp;quot;,&lt;br /&gt;
    &amp;quot;BMX&amp;quot;,&lt;br /&gt;
    &amp;quot;Monster Truck&amp;quot;,&lt;br /&gt;
    &amp;quot;Quad&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function outputVehicleCountToServerLog()&lt;br /&gt;
    for index, typeName in ipairs(types) do&lt;br /&gt;
        local count = getVehiclesCountByType(typeName)&lt;br /&gt;
        outputServerLog((&amp;quot;Amount of '%s' vehicles: %d&amp;quot;):format(typeName, count))&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
addEventHandler(&amp;quot;onResourceStart&amp;quot;, resourceRoot,&lt;br /&gt;
    function ()&lt;br /&gt;
        outputVehicleCountToServerLog()&lt;br /&gt;
        setTimer(outputVehicleCountToServerLog, 60e3, 0)&lt;br /&gt;
    end,&lt;br /&gt;
false)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Example 2==&lt;br /&gt;
&amp;lt;section name=&amp;quot;Server&amp;quot; class=&amp;quot;server&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
This example writes the amount of vehicles on the specified type, and sends the results to the server console.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function getVehiclesCountByType(vehicleType)&lt;br /&gt;
    assert(type(vehicleType) == &amp;quot;string&amp;quot;, &amp;quot;expected string at argument 1, got &amp;quot;.. type(vehicleType))&lt;br /&gt;
 &lt;br /&gt;
    local getVehicleType = getVehicleType -- Localize&lt;br /&gt;
    local vehicleList = getElementsByType(&amp;quot;vehicle&amp;quot;)&lt;br /&gt;
    local vehicleCount = #vehicleList&lt;br /&gt;
    local typeCount = 0&lt;br /&gt;
 &lt;br /&gt;
    for index = 1, vehicleCount do&lt;br /&gt;
        if getVehicleType(vehicleList[index]) == vehicleType then&lt;br /&gt;
            typeCount = typeCount + 1&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    return typeCount&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function IfElse(condition, resultTrue, resultFalse)&lt;br /&gt;
      if (not condition) then&lt;br /&gt;
      return resultFalse&lt;br /&gt;
else&lt;br /&gt;
      return resultTrue&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local stringType = { [&amp;quot;Automobile&amp;quot;] = true,&lt;br /&gt;
                     [&amp;quot;Plane&amp;quot;] = true,&lt;br /&gt;
                     [&amp;quot;Bike&amp;quot;] = true,&lt;br /&gt;
                     [&amp;quot;Helicopter&amp;quot;] = true,&lt;br /&gt;
                     [&amp;quot;Boat&amp;quot;] = true,&lt;br /&gt;
                     [&amp;quot;Train&amp;quot;] = true,&lt;br /&gt;
                     [&amp;quot;Trailer&amp;quot;] = true,&lt;br /&gt;
                     [&amp;quot;BMX&amp;quot;] = true,&lt;br /&gt;
                     [&amp;quot;Monster Truck&amp;quot;] = true,&lt;br /&gt;
                     [&amp;quot;Quad&amp;quot;] = true&lt;br /&gt;
                   }&lt;br /&gt;
&lt;br /&gt;
addCommandHandler(&amp;quot;amountvehicle&amp;quot;, function(aElement, commandName, typevehicle)&lt;br /&gt;
                   if (getElementType(aElement) == &amp;quot;console&amp;quot;) then&lt;br /&gt;
                   if (type(typevehicle) ~= &amp;quot;string&amp;quot;) or (tostring(typevehicle) == nil) or (tostring(typevehicle) == &amp;quot;&amp;quot;) or (string.len(typevehicle) &amp;lt; 3) then&lt;br /&gt;
                      outputServerLog(&amp;quot;Correct syntax: amountvehicle &amp;lt;Type Vehicle&amp;gt;&amp;quot;)&lt;br /&gt;
                return&lt;br /&gt;
                   end&lt;br /&gt;
         if stringType[tostring(typevehicle)] then&lt;br /&gt;
         outputServerLog((&amp;quot;[Vehicles By Type - '%s'] - &amp;quot;..IfElse(getVehiclesCountByType(typevehicle) &amp;lt; 1, &amp;quot; Not found!&amp;quot;, &amp;quot;Found '%s'&amp;quot;)):format(typevehicle, getVehiclesCountByType(typevehicle)))&lt;br /&gt;
else&lt;br /&gt;
         outputServerLog(&amp;quot;#ERROR! The entered this syntax - '&amp;quot;..tostring(typevehicle)..&amp;quot;' isn't correct&amp;quot;)&lt;br /&gt;
      end&lt;br /&gt;
   end&lt;br /&gt;
end)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
{{Useful Functions}}&lt;/div&gt;</summary>
		<author><name>EnternalEnvy</name></author>
	</entry>
</feed>