Jump to content

getPlayerCount


Recommended Posts

hello, I wanted to know if I can use this function this way ?

getPlayerCount, dimension 2

====count players in dimension 2====

addEvent("text", true) 
addEventHandler("text", getRootElement(), 
function() 
    for k,v in ipairs(getElementsByType("player")) do 
        if getElementDimension ( v ) == 2 then 
            local Count = getPlayerCount(v) 
            setElementData(source,"players",Count) 
        end 
    end 
end) 

Link to comment

Add a variable for player count. Like that,

addEvent("text", true) 
addEventHandler("text", getRootElement(), 
function() 
   local playerCount = getPlayerCountInDimension( 2 ) 
   if playerCount then 
         setElementData(source,"players",Count) 
   end 
end) 
  
function getPlayerCountInDimension( dimID ) 
    local counter = 0 
    for k,v in ipairs(getElementsByType("player")) do 
        if getElementDimension ( v ) == dimID then 
            counter = counter + 1 
        end 
    end 
    return counter 
end 

Link to comment
  • Moderators

I think you wanted to do more like this:

addEvent("text", true) 
addEventHandler("text", getRootElement(), 
function() 
    local count = 0 
    for k, v in ipairs(getElementsByType("player")) do 
        if getElementDimension ( v ) == 2 then 
            count = count + 1 
        end 
    end 
    setElementData(source, "players", count) 
end) 

Or as a function extension:

local _getPlayerCount = getPlayerCount 
function getPlayerCount( dim ) 
    if dim == nil then return _getPlayerCount() end 
    local count = 0 
    for k, p in ipairs(getElementsByType("player")) do 
        if getElementDimension(p) == dim then 
            count = count + 1 
        end 
    end 
end 

That you will then call like this:

addEvent("text", true) 
addEventHandler("text", getRootElement(), 
function() 
    local count = getPlayerCount(2) 
    setElementData(source, "players", count) 
end) 

But your code doesn't make sense as it looks like you want to output a message to all players in a specific dimension ? In that case, getting the numbers of players in that dimension won't help you. You will need the list of the players in that dimension instead:

function getPlayersInDimension( dim ) 
    local players = {} 
    for k, p in ipairs(getElementsByType("player")) do 
        if getElementDimension(p) == dim then 
            table.insert(players, p) 
        end 
    end 
    return players 
end 

And then use it like this:

addEvent("text", true) 
addEventHandler("text", getRootElement(), 
function( msg ) 
    local players = getPlayersInDimension(2) 
    for k, p in ipairs(players) do 
        outputChatBox(p, msg) 
    end 
end) 

Don't forget to send the text message as argument of your triggerServerEvent.

More informations on what you try to achieve would be appreciated.

Best regards,

Citizen

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