Jump to content

Counting the players which have the element data


Karuzo

Recommended Posts

Hey Guys,

i'm trying myself in a lobby script and i want to count how much players are in this lobby. I'm setting an element data to those players. So is there a possibility to count the players which have this element data ?

Didn't found anything related to that.

Thank you for your help.

Link to comment
function getLobbyPlayers() 
    local lobbyPlayers = {} 
    for i,v in ipairs(getElementsByType("player")) do 
        if getElementData(v, "YOUR ELEMENTDATA HERE") == YOUR VALUE HERE then 
            table.insert(lobbyPlayers, v) 
        end 
    end 
    return lobbyPlayers 
end 

local numberOfPlayers = #getLobbyPlayers() -- This is the amount of players in the lobby 
local playersInLobby = getLobbyPlayers() -- This is a table with all the players in the lobby 

Link to comment

Why don't you do it with teams? Its more efficient.

If you want to know the total players in your server then obviously use:

getElementsByType("player")  

If you want to check how many players are in a arena then:

local pTcount = getPlayersInTeam ( getTeamFromName("TEAM NAME") ) 
if #pTcount == NUMBER then 
-- some code 
end  

In case you want to start a another map for a specified arena.

addEventHandler("onPlayerWasted", getRootElement(), 
function () 
local pTname = getTeamName ( getPlayerTeam(source) ) 
local pTcount = getPlayersInTeam ( pTname ) 
if #pTcount > 0 then 
-- free camera  
elseif #pTcount == 0 then 
-- start another map  
     end  
          end )  

Link to comment
Thank you!

And how should i remove a player which has left the lobby ?

Like:

Set the element data to true if hes in the lobby,

and set it to false if he leaves?

And then just do table.remove?

You could just retrieve all the players again, it'll clear the table and then send you all the players which are still in the lobby.

Link to comment

I'd like to share my another piece of code with you because you might have problems later on:

If you want to wrap all players into vehicles when you create objects and shits then:

  
local spawner = { } -- Insert all your vehicles into this table 
  
function  WarpAllPedIntoVehNpickup( theTeam ) 
for i, v in pairs( getPlayersInTeam (getTeamFromName (theTeam) )) do 
        setElementDimension ( v, 1 ) -- replace it with what ever you want 
        local carR = spawner[ math.random( 1,#spawner) ]  
        setElementAlpha ( carR, 255 ) 
local ab = warpPedIntoVehicle ( v, carR )   
if ab then 
table.remove ( spawner, carR) -- remove it so people don't spawn in the same vehicle but the function will be still running  
 end 
end 
return true 
end 
  
WarpAllPedIntoVehNpickup ( "team" )  

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