Jump to content

[HELP] Get the number of people in a vehicle


Recommended Posts

Im not sure, But try:

occupants = getVehicleOccupants(vehicle) -- Get the ocuppants on the car 
numseats = getVehicleMaxPassengers(getPedOccupiedVehicle ( thePlayer )) -- take the player vehicle 
  
if (unpack(ocuppants) == numseats) then   -- verify if the vehicle its full 

Link to comment
  • MTA Team
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.

That's why you can't use '#' length operator on the result table.

Link to comment
local occupants = getVehicleOccupants(vehicle) 
amount = 0 
for s, o in pairs(occupants) do  
local amounta = amount 
amount = amounta + 1 
end 

That worked, thanks..

I have another problem now...

Is there a way to find out if there is a free seat in a vehicle and make the ped enter the car as a passenger? This is what I've done and the ped doesn't enter the vehicle even if they are close to it:

function makeGirlfriendEnterVehicle(thePlayer, seat, jacked) 
    local vehicle = getPlayerOccupiedVehicle(thePlayer) 
    local occupants = getVehicleOccupants(vehicle) 
    amount = 0 
    for s, o in pairs(occupants) do 
        local amounta = amount 
        amount = amounta + 1 
    end 
    local maxPassengers = getVehicleMaxPassengers(vehicle) 
    if not (getElementData(thePlayer, "gfFollow") == nil) then 
        for i, v in ipairs(getElementsByType("ped")) do 
            if (getElementData(v, "name") == getElementData(thePlayer, "hasGirlfriend")) and (getElementData(v, "type") == "girlfriend") then 
                girlFriendPed = v 
            end 
        end 
        if (maxPassengers > amount) then 
            amount = 0 
            triggerClientEvent ( "girlfriend_EnterCar", girlFriendPed ) 
        end 
    else 
        return 
    end 
end 
addEventHandler ( "onVehicleStartEnter", getRootElement(), makeGirlfriendEnterVehicle ) 
  
addEvent( "girlfriend_EnterCar", true ) 
function BEnterCar ( ) 
    if (isElement(source)) then 
        setPedControlState( source, "enter_passenger", true ) 
    end 
end 
addEventHandler( "girlfriend_EnterCar", getRootElement(), BEnterCar ) 

Link to comment
  • Moderators

[quote name=..:D&G:..]Any ideas why I get "C stack overflow" here?

for s, o in pairs(occupants) do 

You will only get that error when you are calling a function inside this loop, which is related to the function which contains this loop.

This is an example how you get this error:

function testFunction () 
    testFunction() 
end 
  
testFunction() 

Link to comment
[quote name=..:D&G:..]Any ideas why I get "C stack overflow" here?
for s, o in pairs(occupants) do 

You will only get that error when you are calling a function inside this loop, which is related to the function which contains this loop.

This is an example how you get this error:

function testFunction () 
    testFunction() 
end 
  
testFunction() 

But there is no function called inside the loop, there is only the local "amount"

Link to comment

I only use ipairs and this is the other, and only loop is this:

amount = 0 
    for s, o in pairs(occupants) do 
        local amounta = amount 
        amount = amounta + 1 
    end 
    local maxPassengers = getVehicleMaxPassengers(vehicle) 
    if not (getElementData(thePlayer, "gfFollow") == nil) then 
        for i, v in ipairs(getElementsByType("ped")) do 
            if (getElementData(v, "name") == getElementData(thePlayer, "hasGirlfriend")) and (getElementData(v, "type") == "girlfriend") then 
                girlFriendPed = v 
            end 
        end 

Link to comment

Try this?

function enter (vehicle,thePlayer) 
 passengers = 0 
        for seat, player in pairs(getVehicleOccupants(vehicle)) do 
            passengers = passengers + 1 
        end 
  local maxPassengers = getVehicleMaxPassengers(vehicle) 
        if not (getElementData(thePlayer, "gfFollow") == nil) then 
        for i, v in pairs(getElementsByType("ped")) do 
        if (getElementData(v, "name") == getElementData(thePlayer, "hasGirlfriend")) and (getElementData(v, "type") == "girlfriend") then 
             girlFriendPed = v 
             end 
        -- girlFriendPed 
        end 
    end 
             

Link to comment
  • Moderators

+ pairs, loops through custom indexes, like:

local tableAsIndex = {} 
  
local thisTable = { 
["customIndex1"] = 45836, 
["customIndex2"] = 45836, 
[true] = 3246765, 
[localPlayer] = 547356, 
[resourceRoot] = 5646745387, 
[tableAsIndex] = 354654638 
-- etc. 
} 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...