Jump to content

Get Players On Team


PoliceSA

Recommended Posts

Posted

Yes, first of all you need to get all player elements and then check their team. After that, add them to a table. For example;

-- Define the table. 
local mtaTeamPlayers = {}; 
  
-- First of all, fetch all current player elements 
local allPlayers = getElementsByType("player"); 
-- Then loop through all players and check their team name 
for k, v in ipairs(allPlayers) do 
    if(getTeamName(getPlayerTeam(v)) == "MTA") then 
        -- If their team name matches the one you want, get their player name and store it in a local variable 
        local playerName = getPlayerName(v); 
        -- Insert their name into our previously defined table 
        table.insert(mtaTeamPlayers, #mtaTeamPlayers, playerName); 
    end 
end 

Of course that's just an example. You'll have to make code which updates the table whenever a player join and quits. You'll also have to loop through the table to draw their names using dxDrawText.

Edit:

Or use the function manawydan suggested :P

Posted

Aahah thx :D but what should I put in the client? Cuz i'm not good to script

i should use on server like that?

for k, v in ipairs(allPlayers) do 
    if(getTeamName(getPlayerTeam(v)) == "team[1]") then 
        local playerName = getPlayerName(v) 
        table.insert(mtaTeamPlayers, #mtaTeamPlayers, playerName); 
        triggerClientEvent (source, "TriggerToClient", g_Root, mtaTeamPlayers) 
              end 
           end 

Posted

one example,

addEventHandler("onClientRender",root, 
function() 
local team = getPlayerTeam(localPlayer) 
if not(team)then return end 
local players = getPlayersInTeam(team) 
for k,v in ipairs(players)do 
-- you dxdraw here 
end 
end) 
  

Posted
  
.... 
local team = getPlayerTeam(localPlayer) 
if not(team)then return end 
local players = getPlayersInTeam(team) 
for k,v in ipairs(players)do 
dxDrawText (whatishouldputhere?, x + 60, 420, x, y, tocolor( 255, 255, 255 ), 1, "default-bold" ) 
end 

Posted (edited)
  
addEventHandler("onClientRender",root, 
function () 
local team = getPlayerTeam(localPlayer) 
if not(team)then return end 
local players = getPlayersInTeam(team) 
for _,v in ipairs(players)do 
dxDrawText (getPlayerName(v), x + 60, 420, x, y, tocolor( 255, 255, 255 ), 1, "default-bold" ) 
end 

Edited by Guest
  • MTA Team
Posted

You don't move the names. Every name will be rendered on the same spot. You should use the index value (that one you called _)

Posted

Like That?

  
.... 
for id,v in ipairs(players)do 
         dxDrawText(getPlayerName(v), x + 60,420*id, x, screenHeight, tocolor(255, 255, 255, 255), 1, "default-bold", "left", "top", false, false, true, true, false) 

  • MTA Team
Posted

Yeah, almost.

With your code your first name will be rendered at height 420, the next one at 840 and so on. (id starts at 1)

Solution:

dxDrawText(..., x + 60, 420 + 30 * (id - 1), ...) 

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