Jump to content

dx players list


Recommended Posts

Posted
function showPlayersList()
local all_players = getElementsByType("player")
for num,this_player in ipairs(all_players) do
local plname = getPlayerName(this_player)
dxDrawText(plname,100,100+num*16) --num increases with each cycle, so every name is drawn under previous name
end
end
addEventHandler("onClientRender",getRootElement(),showPlayersList)

Maybe this will help you to start.

Posted

Or you could cache the player list in a table on resource start and every time someone joins/leaves, you modify the table, so you wouldn't have to getElementsByType("player") every frame.

local players = {} -- make it local to the file, not necessary, but I like
function cachePlayers() 
for k, v in ipairs(getElementsByType("player")) do
	players[k] = v
end
end
 
function addPlayer()
table.insert(players,source)
end
function removePlayer()
for k,v in ipairs(players) do
if v == source then
table.remove(players,k)
return
end
end
end	
function drawList()
for num,this_player in ipairs(players) do
local plname = getPlayerName(this_player)
dxDrawText(plname,100,100+num*16) --num increases with each cycle, so every name is drawn under previous name
end
end
addEventHandler("onClientRender",root,drawList)
addEventHandler("onClientResourceStart",resourceRoot,cachePlayers)
addEventHandler("onClientPlayerJoin",root,addPlayer) -- add a player into the table when someone joins
addEventHandler("onClientPlayerQuit",root,removePlayer) -- remove a player from table if he quits

It's slightly more complicated but should be faster. Not much, but if you have lot's of stuff going on onClientRender, things should be optimized.

Posted

Thanks guys it worked!

Edit: why this dosnt work

function drawList()
for num,this_player in ipairs(players) do
  r, g, b = getPlayerNametagColor( this_player )
local plname = getPlayerName(this_player)
dxDrawText(plname,300,150+num*16,tocolor( r or 255, g or 255, b or 255, 255 ))
end
end

i try to set any color and nothing works. :roll:

Posted
You omitted the right and bottom arguments of dxDrawText.

Errors should have told you that.

You could have easily fixed that by yourself.

https://wiki.multitheftauto.com/wiki/DxDrawText

If i put like this

function drawList()
for num,this_player in ipairs(players) do
local plname = getPlayerName(this_player)
local r, g, b = getPlayerNametagColor( this_player )
dxDrawText(plname,300,150+num*16,tocolor( r or 255, g or 255, b or 255, 255 ),1.0,"default","left","top",false,false,false)
end
end

text dosnt even appear :oops:

Posted

If it doesn't appear, you must be getting a warning/error message. Try to make a new variable which will be the colour (value returned from tocolor) and check what it is (debug the script...).

Posted
If it doesn't appear, you must be getting a warning/error message. Try to make a new variable which will be the colour (value returned from tocolor) and check what it is (debug the script...).

I got everytime i script the debugscript on and no errors comes.

Posted

You have added an event handler, haven't you (use onClientRender)? If not, add this to the bottom; addEventHandler("onClientRender",getRootElement(),drawList)

Posted
You have added an event handler, haven't you (use onClientRender)? If not, add this to the bottom; addEventHandler("onClientRender",getRootElement(),drawList)

I have already this.

Posted

If you had just paid attention to what I told you, it would have been fixed.

You're missing 2 arguments in dxDrawText.

It's unbelievably easy to fix, I won't fix it for you.

Posted
If you had just paid attention to what I told you, it would have been fixed.

You're missing 2 arguments in dxDrawText.

It's unbelievably easy to fix, I won't fix it for you.

ahhhhhhhh i found it, thanks!

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