Jump to content

SOLVED problem, amount of occupants


#RooTs

Recommended Posts

Posted

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) 
  

Posted

On line 13, you break the loop just after first if-else. This means it will stop looping after the first seat without an occupant. Remove 'break' from that line (or the whole line as it's unnecessary)

Posted (edited)

friends or @MrTasty, is possible create a table to pull all Occupants the vehicle

Example

Code Removed

that will work ?

Edited by Guest
Posted (edited)
Yeah, that should work

what is the correct function, to pull all occupants in one number?

getVehicleOccupant 
-- or 
getVehicleOccupants 

the example that I made is the right job?

Code Removed

Edited by Guest
Posted

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 ?

Posted
 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?

Posted
local cnt = #getVehicleOccupants ( getPedOccupiedVehicle ( localPlayer ) ) + 1; 

:)

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

Posted
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.

Posted (edited)
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
Posted (edited)
Code Removed

Is this what you're looking for?

I had already tested the his first example in first post. this will work?

@n3wage, I will test your sample also

Edited by Guest
  • Moderators
Posted

No it doesn't.

  • You have to check if the vehicle does exist.
  • Line 4 misplaced, must be de defined before the loop.
  • Line 8 and 9 misplaced and not used.

Posted
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?

Posted
[...]

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: .

Posted
[...]

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.

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

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