Jump to content

[HELP] getOnlinePlayers


ViRuZGamiing

Recommended Posts

Posted
local onlinePlayers = {} 
  
for i, v in ipairs ( getElementsByType ( "player" ) ) do 
    table.insert ( onlinePlayers, v ) 
end 

Owner of [HUN]Magyar Play Szerver 1.4, IP: 31.220.43.153:22003

Posted

Can I show all players onCommand?

addEventHandler("online", 
function () 
 --show online players part 
end 
) 

"If debugging is the process of removing software bugs, then programming must be the process of putting them in."

Posted

Yes, you can. Here is a simple example:

addCommandHandler ( "online", 
    function () 
        for i, v in ipairs ( getElementsByType ( "player" ) ) do 
            outputChatBox ( getPlayerName ( v ) ) 
        end 
    end ) 

Owner of [HUN]Magyar Play Szerver 1.4, IP: 31.220.43.153:22003

Posted

So you want more names in a line?

local charLimit = 50 -- maximum charachters in a line 
  
addCommandHandler ( "online",  
    function () 
        local line = {} 
        local currLength = 0 
        for i, v in ipairs ( getElementsByType ( "player" ) ) do 
            local name = getPlayerName ( v ) 
            if currLength + #name > charLimit then 
                outputChatBox ( table.concat ( line, ", " ) ) 
                line = {} 
                currLength = 0 
            end 
            table.insert ( line, name ) 
            currLength = currLength + #name 
        end 
    end ) 

Owner of [HUN]Magyar Play Szerver 1.4, IP: 31.220.43.153:22003

Posted (edited)
  
function getOnlinePlayers() 
  return getElementsByType("player") 
end 
  
function listOnlinePlayers(player) 
  local players = getOnlinePlayers() 
  local playerNames = { } 
  for i=1,#players do 
    table.insert(playerNames,getPlayerName(players[i])) 
  end 
  local text = table.concat(playerNames,", ") -- Separate each player with comma 
  outputChatBox(text,getRootElement()) 
end 

Edited by Guest
Posted

I've tested

addCommandHandler ( "online", 
    function () 
        for i, v in ipairs ( getElementsByType ( "player" ) ) do 
            outputChatBox ( getPlayerName ( v ) ) 
        end 
    end ) 

but i'm currently alone on a LAN Server what if i'm with 2 or more how would it be displayed?

1 Name a line? All behind eachother with just a space in between?

greetz viruz

"If debugging is the process of removing software bugs, then programming must be the process of putting them in."

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