Jump to content

SOLVED problem, amount of occupants


#RooTs

Recommended Posts

Try This:

  
function render() 
if isPedInVehicle(localPlayer) then 
local vehicle = getPedOccupiedVehicle (localPlayer) 
local occupants = getVehicleOccupants(vehicle) 
local PlayerName = getPlayerName(localPlayer) 
local VehicleName = getVehicleName (vehicle) 
    local cnt = 0 
    for i=0, 3 do 
         
        if occupants[i] and isElement(occupants[i]) then         
                cnt = cnt + 1 
        else break 
        end 
        
        dxDrawText(" #FF0000PASSAGEIROS: #FFFFFF" .. tostring(cnt) .. "/4 ", x*870, y*640, x*716, y*351, tocolor(255, 255, 255), x*1.7, "default-bold", "center", "top", false, false, false, true) 
    end 
end 
end 
addEventHandler("onClientRender", root, render) 
  

Link to comment

my function not work

local AllOccupant = {0,1,2,3,4} 
  
local check = 0 
for _, v in ipairs (AllOccupant) do 
     car = getPedOccupiedVehicle(player) 
     check = check + getVehicleOccupant (car,v) 
end 
  
dxDrawText(check.....) 

someone help me ?

Link to comment
 function render() 
    local vehicle = getPedOccupiedVehicle(getLocalPlayer()) 
    for _, occupant in pairs(getVehicleOccupants(vehicle)) do 
        local cnt = 0 
        if (occupant and getElementType(occupant) == "player") then         
                cnt = cnt + 1 
        end 
        local PlayerName = getPlayerName(getLocalPlayer()) 
        local VehicleName = getVehicleName (vehicle) 
    end 
    dxDrawText(" #FF0000PASSAGEIROS: #FFFFFF" .. tostring(cnt) .. "/" .. tostring(getVehicleMaxPassengers(vehicle)), x*870, y*640, x*716, y*351, tocolor(255, 255, 255), x*1.7, "default-bold", "center", "top", false, false, false, true) 
end 
addEventHandler("onClientRender", root, render) 
  

Is this what you're looking for?

Link to comment
No. getVehicleOccupants returns a key table, not an index table. Your hashtag will not work.

That would be a problem if the keys were strings or everything else, with numbers the hashtag works very well.

returns = { -- getVehicleOccupants returns 
    [0] = "a", 
    [1] = "b", 
    [2] = "c", 
    [3] = "d" 
} 
  
print ( #returns ) 

Prints:

H7OqqrP.png

+1 = the amount of occupants.

Link to comment
No. getVehicleOccupants returns a key table, not an index table. Your hashtag will not work.

That would be a problem if the keys were strings or everything else, with numbers the hashtag works very well.

returns = { -- getVehicleOccupants returns 
    [0] = "a", 
    [1] = "b", 
    [2] = "c", 
    [3] = "d" 
} 
  
print ( #returns ) 

Prints:

H7OqqrP.png

+1 = the amount of occupants.

That's what we call a hack.

You should learn Lua and English before throwing crap like that here. If there is no occupant in a seat, it will have no index, meaning your hashtag method will not work since it cannot count non-index tables. It is called a key table when it's not purely indexed, having 0, 2, 5, 10 instead of 1, 2, 3, 4, etc. Wish to see it in action?

0 from

print( #{ [ 0 ] = true, [ 2 ] = true } ) 

You can mind blow with this code

addEventHandler( "onResourceStart", resourceRoot, 
    function( ) 
        setTimer( function( ) 
            local function count( table ) 
                local count = 0 
                 
                for _ in pairs( table ) do 
                    count = count + 1 
                end 
                 
                return count 
            end 
             
            local vehicle = createVehicle( getVehicleModelFromName( "Premier" ), 0, 0, 3 ) 
             
            if ( isElement( vehicle ) ) then 
                print( "vehicle created <" .. tostring( vehicle ) .. ">" ) 
                 
                local peds = 10 
                 
                for i = 0, peds - 1, 2 do 
                    local ped = createPed( 0, 0 + i, 0, 3 ) 
                     
                    if ( isElement( ped ) ) then 
                        print( "ped created <" .. tostring( ped ) .. ">" ) 
                         
                        if ( not warpPedIntoVehicle( ped, vehicle, i ) ) then 
                            print( "ped <" .. tostring( ped ) .. "> NOT warped to vehicle <" .. tostring( vehicle ) .. "> at slot " .. i ) 
                             
                            local occupants = getVehicleOccupants( vehicle ) 
                             
                            print( ">> vehicle occupants: #occupants => " .. #occupants ) 
                            print( ">> vehicle occupants: count( occupants ) => " .. count( occupants ) ) 
                             
                            print( " begin" ) 
                             
                            for i, v in ipairs( occupants ) do 
                                print( " vehicle occupant: " .. i .. ": " .. tostring( v ) ) 
                            end 
                             
                            print( " end" ) 
                             
                            print( " begin" ) 
                             
                            for i, v in pairs( occupants ) do 
                                print( " vehicle occupant: " .. i .. ": " .. tostring( v ) ) 
                            end 
                             
                            print( " end" ) 
                             
                            local elements = { "ped", "vehicle" } 
                             
                            for i = 1, #elements do 
                                local elements = getElementsByType( elements[ i ], resourceRoot ) 
                                 
                                for i = 1, #elements do 
                                    destroyElement( elements[ i ] ) 
                                end 
                            end 
                             
                            break 
                        else 
                            print( "ped <" .. tostring( ped ) .. "> warped to vehicle <" .. tostring( vehicle ) .. "> at slot " .. i ) 
                        end 
                    end 
                end 
            end 
        end, 50, 1 ) 
    end 
) 

Edited by Guest
  • Thanks 1
Link to comment
addEventHandler("onClientRender", root, 
    function() 
        if isPlayerInVehicle(getLocalPlayer()) 
            local vehicle = getPedOccupiedVehicle(getLocalPlayer() 
            local cnt = 0 
             
            for _, occupant in pairs(getVehicleOccupants(vehicle)) do 
                if occupant then 
                    if getElementType(occupant) == 'player' then 
                        cnt = cnt + 1 
                    end 
                end 
            end 
        end 
    end 
) 

What do you need the names of the player and vehicle for?

Link to comment
[...]

You should learn Lua and English before throwing crap like that here.

[...]

Why all this arrogance? Ok, i made a mistake, this is normal, you could have just explained why my code wouldn't work without 'disrespecting' me, You don't even know who am i.

Also my English isn't perfect, this is a fact, But I have the same right as you to post here, Have a great day :wink: .

Link to comment
[...]

You should learn Lua and English before throwing crap like that here.

[...]

Why all this arrogance? Ok, i made a mistake, this is normal, you could have just explained why my code wouldn't work without 'disrespecting' me, You don't even know who am i.

Also my English isn't perfect, this is a fact, But I have the same right as you to post here, Have a great day :wink: .

Because you were replying as if you knew exactly what you were doing. Apparently you didn't, but there you go.

Link to comment
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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