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 ) …')
 
No edit summary
 
(10 intermediate revisions by 7 users not shown)
Line 1: Line 1:
__NOTOC__
{{Server client function}}
{{Server client function}}
__NOTOC__
This function gets all peds sitting in the specified vehicle.
This function gets all players sitting in the specified vehicle.


==Syntax==  
==Syntax==  
Line 7: Line 7:
table getVehicleOccupants ( vehicle theVehicle )             
table getVehicleOccupants ( vehicle theVehicle )             
</syntaxhighlight>  
</syntaxhighlight>  
 
{{OOP||[[vehicle]]:getOccupants|occupants}}
===Required Arguments===  
===Required Arguments===  
*'''theVehicle:''' The vehicle of which you wish to retrieve the occupants.
*'''theVehicle:''' the [[vehicle]] of which you wish to retrieve the occupants.


===Returns===
===Returns===
Returns a [[table]] with contents...
Returns a [[table]] with seat ID as an index and the occupant as an element like this: table[seat] = occupant


table[seat] = occupant
Returns ''false'' if an invalid vehicle was passed or if the vehicle has no seats (like a trailer)


... if successful. Returns ''false'' in the case of failure.
<div style='font-weight: bold;background:blue;color:white;padding:2px; padding-left:8px;'>COUNTING PLAYERS IN A VEHICLE</div>
<div style='border: 2px solid blue;padding: 5px;'>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. You should use a pairs loop instead.
<syntaxhighlight lang="lua">
local counter = 0


'''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.
for seat, player in pairs(getVehicleOccupants(pseudoVehicle)) do
    counter = counter + 1
end
 
outputDebugString("Players in your vehicle: ".. counter)
</syntaxhighlight>
</div>


==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()
     local veh = getPedOccupiedVehicle(player) -- Get the vehicle the player is in
     -- Make sure they're in a vehicle
     if veh then -- If he is actually in a vehicle...
     if (not isPedInVehicle(localPlayer)) then
        local occupants = getVehicleOccupants(veh) -- Get all vehicle occupants
        outputConsole("You're not in a vehicle.")
         local seats = getVehicleMaxPassengers(veh) -- Get the amount of passenger seats
         return false
       
    end
        outputConsole("------------------------------------",player) -- Print a seprerator for easier reading
    outputConsole("------------------------------------") -- Print a separator for easier reading
       
    for seat, occupant in pairs(getVehicleOccupants(getPedOccupiedVehicle(localPlayer))) do -- Loop through the array
        for seat = 0, seats do -- Repeat with seat = 0, incrementing until it reaches the amount of passenger seats the vehicle has
        outputConsole("Seat " .. seat .. ": " .. getPlayerName(occupant)) -- Print who's in the seat  
            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 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
    outputConsole("------------------------------------") -- Print another separator
end
end
addCommandHandler("occupants",outputOccupants) -- Add a command "/occupants" which triggers outputOccupants
addCommandHandler("occupants", outputOccupants, false, false) -- Add a command "/occupants" which triggers outputOccupants
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Vehicle_functions}}
{{Vehicle_functions}}

Latest revision as of 08:24, 22 October 2019

This function gets all peds sitting in the specified vehicle.

Syntax

table getVehicleOccupants ( vehicle theVehicle )            

OOP Syntax Help! I don't understand this!

Method: vehicle:getOccupants(...)
Variable: .occupants


Required Arguments

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

Returns

Returns a table with seat ID as an index and the occupant as an element like this: table[seat] = occupant

Returns false if an invalid vehicle was passed or if the vehicle has no seats (like a trailer)

COUNTING PLAYERS IN A VEHICLE
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. You should use a pairs loop instead.
local counter = 0

for seat, player in pairs(getVehicleOccupants(pseudoVehicle)) do
    counter = counter + 1
end

outputDebugString("Players in your vehicle: ".. counter)

Example

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

function outputOccupants()
    -- Make sure they're in a vehicle
    if (not isPedInVehicle(localPlayer)) then
        outputConsole("You're not in a vehicle.")
        return false
    end
    outputConsole("------------------------------------") -- Print a separator for easier reading
    for seat, occupant in pairs(getVehicleOccupants(getPedOccupiedVehicle(localPlayer))) do -- Loop through the array
        outputConsole("Seat " .. seat .. ": " .. getPlayerName(occupant)) -- Print who's in the seat 
    end
    outputConsole("------------------------------------") -- Print another separator
end
addCommandHandler("occupants", outputOccupants, false, false) -- Add a command "/occupants" which triggers outputOccupants

See Also

Shared