GetVehicleOccupants: Difference between revisions
Jump to navigation
Jump to search
m (Made example prettier) |
No edit summary |
||
Line 12: | Line 12: | ||
===Returns=== | ===Returns=== | ||
Returns a [[table]] with | 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) | |||
'''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. You should use a pairs loop instead. | '''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. You should use a pairs loop instead. | ||
Line 24: | Line 22: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function outputOccupants(player) | function outputOccupants(player) | ||
if isPedInVehicle(player) then | -- Make sure they're in a vehicle | ||
outputConsole("------------------------------------", player) -- Print a | if (not isPedInVehicle(player)) then | ||
outputConsole("You're not in a vehicle.", player) | |||
return false | |||
end | |||
outputConsole("------------------------------------", player) -- Print a separator for easier reading | |||
local vehicle = getPedOccupiedVehicle(player) | |||
local occupants = getVehicleOccupants(vehicle) or {} -- In case it returned false, loop an empty table. | |||
for seat, occupant in pairs(occupants) do -- Loop through the array | |||
if (occupant and getElementType(occupant) == "player") then -- Make sure the occupant is a player | |||
outputConsole("Seat " .. seat .. ": " .. getPlayerName(occupant), player) -- Print who's in the seat | |||
end | end | ||
end | end | ||
outputConsole("------------------------------------", player) -- Print another separator | |||
end | end | ||
addCommandHandler("occupants",outputOccupants) -- Add a command "/occupants" which triggers outputOccupants | addCommandHandler("occupants", outputOccupants) -- Add a command "/occupants" which triggers outputOccupants | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==See Also== | ==See Also== | ||
{{Vehicle_functions}} | {{Vehicle_functions}} |
Revision as of 15:28, 31 July 2014
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 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)
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. You should use a pairs loop instead.
Example
This example prints all vehicle occupants into the F8 console if "/occupants" is typed:
function outputOccupants(player) -- Make sure they're in a vehicle if (not isPedInVehicle(player)) then outputConsole("You're not in a vehicle.", player) return false end outputConsole("------------------------------------", player) -- Print a separator for easier reading local vehicle = getPedOccupiedVehicle(player) local occupants = getVehicleOccupants(vehicle) or {} -- In case it returned false, loop an empty table. for seat, occupant in pairs(occupants) do -- Loop through the array if (occupant and getElementType(occupant) == "player") then -- Make sure the occupant is a player outputConsole("Seat " .. seat .. ": " .. getPlayerName(occupant), player) -- Print who's in the seat end end outputConsole("------------------------------------", player) -- Print another separator end addCommandHandler("occupants", outputOccupants) -- Add a command "/occupants" which triggers outputOccupants
See Also
- addVehicleUpgrade
- attachTrailerToVehicle
- blowVehicle
- createVehicle
- detachTrailerFromVehicle
- fixVehicle
- getOriginalHandling
- getTrainDirection
- getTrainPosition
- getTrainSpeed
- getTrainTrack
- getVehicleColor
- getVehicleCompatibleUpgrades
- getVehicleController
- getVehicleDoorOpenRatio
- getVehicleDoorState
- getVehicleEngineState
- getVehicleHandling
- getVehicleHeadLightColor
- getVehicleLandingGearDown
- getVehicleLightState
- getVehicleMaxPassengers
- getVehicleModelFromName
- getVehicleName
- getVehicleNameFromModel
- getVehicleOccupant
- getVehicleOccupants
- getVehicleOverrideLights
- getVehiclePaintjob
- getVehiclePanelState
- getVehiclePlateText
- getVehicleSirenParams
- getVehicleSirens
- getVehicleSirensOn
- getVehicleTowedByVehicle
- getVehicleTowingVehicle
- getVehicleTurretPosition
- getVehicleType
- getVehicleUpgradeOnSlot
- getVehicleUpgradeSlotName
- getVehicleUpgrades
- getVehicleVariant
- getVehicleWheelStates
- isTrainDerailable
- isTrainDerailed
- isVehicleBlown
- isVehicleDamageProof
- isVehicleFuelTankExplodable
- isVehicleLocked
- isVehicleOnGround
- isVehicleTaxiLightOn
- removeVehicleUpgrade
- setTrainDerailable
- setTrainDerailed
- setTrainDirection
- setTrainPosition
- setTrainSpeed
- setTrainTrack
- setVehicleColor
- setVehicleDamageProof
- setVehicleDoorOpenRatio
- setVehicleDoorState
- setVehicleDoorsUndamageable
- setVehicleEngineState
- setVehicleFuelTankExplodable
- setVehicleHandling
- setVehicleHeadLightColor
- setVehicleLandingGearDown
- setVehicleLightState
- setVehicleLocked
- setVehicleOverrideLights
- setVehiclePaintjob
- setVehiclePanelState
- setVehiclePlateText
- setVehicleSirens
- setVehicleSirensOn
- setVehicleTaxiLightOn
- setVehicleTurretPosition
- setVehicleVariant
- setVehicleWheelStates