GetVehicleOccupants: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with '{{Server client function}} __NOTOC__ This function gets all players sitting in the specified vehicle. ==Syntax== <syntaxhighlight lang="lua"> table getVehicleOccupants ( vehicle theVehicle ) …')
 
Line 22: Line 22:
==Example==  
==Example==  
This example prints all vehicle occupants into the F8 console if "/occupants" is typed:
This example prints all vehicle occupants into the F8 console if "/occupants" is typed:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">function outputOccupants(player)
function outputOccupants(player)
     if isPedInVehicle(player) then -- If he is actually in a vehicle...
     local veh = getPedOccupiedVehicle(player) -- Get the vehicle the player is in
local veh = getPedOccupiedVehicle(player) -- Get the vehicle the player is in
    if veh then -- If he is actually in a vehicle...
         local occupants = getVehicleOccupants(veh) -- Get all vehicle occupants
         local occupants = getVehicleOccupants(veh) -- Get all vehicle occupants
         local seats = getVehicleMaxPassengers(veh) -- Get the amount of passenger seats
         local seats = getVehicleMaxPassengers(veh) -- Get the amount of passenger seats
       
         outputConsole("------------------------------------",player) -- Print a seprerator for easier reading
         outputConsole("------------------------------------",player) -- Print a seprerator for easier reading
       
         for seat = 0, seats do -- Repeat with seat = 0, incrementing until it reaches the amount of passenger seats the vehicle has
         for seat = 0, seats do -- Repeat with seat = 0, incrementing until it reaches the amount of passenger seats the vehicle has
             local occupant = occupants[seat] -- Get the occupant
             local occupant = occupants[seat] -- Get the occupant
           
             if occupant and getElementType(occupant)=="player" then -- If the seat is occupied by a player...
             if occupant and getElementType(occupant)=="player" then -- If the seat is occupied by a player...
                 occupant = getPlayerName(occupant) -- ... get his name
                 occupant = getPlayerName(occupant) -- ... get his/her name
             elseif occupant and getElementType(occupant)=="ped" then -- If the seat is occupied by a ped...
             elseif occupant and getElementType(occupant)=="ped" then -- If the seat is occupied by a ped...
                 occupant = "<ped>" -- ... clear up there's a ped in the seat
                 occupant = "<ped>" -- ... clear up there's a ped in the seat
Line 41: Line 40:
                 occupant = "<empty>" -- ... clear up that the seat is empty
                 occupant = "<empty>" -- ... clear up that the seat is empty
             end
             end
           
             outputConsole("Seat "..seat..": "..occupant,player) -- Print who's in the seat
             outputConsole("Seat "..seat..": "..occupant,player) -- Print who's in the seat
         end
         end
       
         outputConsole("------------------------------------",player) -- Print another seprerator
         outputConsole("------------------------------------",player) -- Print another seprerator
     end
     end

Revision as of 20:23, 28 April 2012

This function gets all players sitting in the specified vehicle.

Syntax

table getVehicleOccupants ( vehicle theVehicle )            

Required Arguments

  • theVehicle: The vehicle of which you wish to retrieve the occupants.

Returns

Returns a table with contents...

table[seat] = occupant

... if successful. Returns false in the case of failure.

Note: Don't use an ipairs loop with the table returned by this function. It will skip the driver, as ipairs starts at 1 and the driver seat is ID 0. And if there's an empty seat, ipairs will stop looping. Refer to the example for a way to get around this.

Example

This example prints all vehicle occupants into the F8 console if "/occupants" is typed:

function outputOccupants(player)
    if isPedInVehicle(player) then -- If he is actually in a vehicle...
		local veh = getPedOccupiedVehicle(player) -- Get the vehicle the player is in
        local occupants = getVehicleOccupants(veh) -- Get all vehicle occupants
        local seats = getVehicleMaxPassengers(veh) -- Get the amount of passenger seats
 
        outputConsole("------------------------------------",player) -- Print a seprerator for easier reading
 
        for seat = 0, seats do -- Repeat with seat = 0, incrementing until it reaches the amount of passenger seats the vehicle has
            local occupant = occupants[seat] -- Get the occupant
 
            if occupant and getElementType(occupant)=="player" then -- If the seat is occupied by a player...
                occupant = getPlayerName(occupant) -- ... get his/her name
            elseif occupant and getElementType(occupant)=="ped" then -- If the seat is occupied by a ped...
                occupant = "<ped>" -- ... clear up there's a ped in the seat
            else -- If the seat is unoccupied...
                occupant = "<empty>" -- ... clear up that the seat is empty
            end
 
            outputConsole("Seat "..seat..": "..occupant,player) -- Print who's in the seat
        end
 
        outputConsole("------------------------------------",player) -- Print another seprerator
    end
end
addCommandHandler("occupants",outputOccupants) -- Add a command "/occupants" which triggers outputOccupants

See Also