Kicking

Lol yes i have many questions

I have DListView that displays all players



local PlayerList = vgui.Create("DListView")
	PlayerList:SetParent(MainMenu)
	PlayerList:SetPos( MenuWidth / 3 + 30, 50 )
	PlayerList:SetSize( MenuWidth - 250, MenuHeight - 100 )
	PlayerList:SetMultiSelect(false)
	PlayerList:AddColumn("Name")
	PlayerList:AddColumn("Is admin?")
	PlayerList:AddColumn("Is super admin?")
	for k,v in pairs(player.GetAll()) do
		PlayerList:AddLine(v:Nick(),v:IsAdmin(),v:IsSuperAdmin())
	end


Then when a function calls from a button i want to kick the player thats selected



local KickButton = vgui.Create( "DButton" )
	KickButton:SetParent( MainMenu )
	KickButton:SetText( "Kick! (Choose player)" )
	KickButton:SetPos( 20, 200 )
	KickButton:SetSize( MenuWidth / 3, 40 )
	KickButton.DoClick = function ()
		PlayerList:GetSelected():Nick():Kick("")
	end


and i get this



lua:71: attempt to call method 'Nick' (a nil value)


What is the proper way to do this?

What does PlayerList:GetSelected() return?
It sounds like it’s not returning you a player reference, which is why it doesn’t have a Nick() method.

Try to add a debug line to print that line without nick or kick and see what the output is.


KickButton.DoClick = function ()
	--PlayerList:GetSelected():Nick():Kick("")
	-- Untested, HUD_PRINTTALK might be wrong, and the concatenate might break.
	PrintMessage(HUD_PRINTTALK, "Muh Debugs: " .. tostring( PlayerList:GetSelected() )
end

On top of that you cannot kick using a nick.

I did what you seggusted and this is what i got. Not happy with it

table: 0x3ba5db68

So it’s a table (array). Try using the PrintTable function and checking your console to see what is inside it.

using


print(PrintTable(PlayerList:GetSelected()))

(hopefully i did it right)

thats what the console had to say about it
1 = Panel: [name:Panel][class:Panel][1,0,348,17]

-snip-

If you are getting output without errors, then you are doing something right!

It seems the official wiki doesn’t have documentation on these methods, so I use one sometimes that has more information.

So GetSelected() is good for getting a table full of every line selected.
All you need is one line being selected at a time, and GetSelectedLine() returns a reference to the first selected line.

After you have the line reference you then need to look up a way to get data from that list, by a line number.

After you use that line number to get the data, you can then extract their name from the row and issue a kick command for them.

I got this when i tryed to run it

lua:121: ‘)’ expected near ‘then’

[editline]24th December 2013[/editline]

I have no idea how to do that. I looked in the reference page about dlistview but nothing there. A little more help please?

http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index64cf.html

scroll down, and read through the method names. You are trying to get data from a row given a line number from your list.

Read the method names, find some that sound like they might help you, click on them and see what you pass them and what they return to you.

i tried print(PlayerList:GetLine():GetColumnText())

and got

lua:116: attempt to index a nil value

A hint perhaps?

PlayerList:GetLine( int line number )
get line takes an integer as an argument, the line number.

print(PrintTable(PlayerList:GetSelected()))

Before you were using PlayerList:GetSelected() but it returns a table, and you really don’t need that.
Luckly reading on the wiki we found one that returns an integer!

PlayerList:GetSelectedLine()

So if you can combine the line number you get returned, and use it as the argument for GetLine( place argument here ) then if you print the table GetLine returns you should be seeing some data you are interested in.

tried this print(PrintTable(PlayerList:GetLine(PlayerList:GetSelectedLine())))

got this lua:54: bad argument #1 to ‘pairs’ (table expected, got userdata)

very close, just remove the print( )
print will print strings

PrintTable( )
expects a table, and does some work for you, and gives you essentially string output.

So never Print(PrintTable()) use one or the other, depending on if you need to print a string or a table.

removed the print but still got the exact same error message. what the poop!

Err my bad I think I’m retarded, and GetLine does not return a table?
Yes.


print( PlayerList:GetLine( PlayerList:GetSelectedLine():GetValue(1) ) )

this should print out the first column of data on the selected line.
So GetValue needs to have the column number that has the players name. I just threw in a 1 to give you something that actually worked.

Errrr Maybe it’s not quite like that. That shouldn’t work. It’s 7 am and I can’t sleep right now. Give me a moment.


print( PlayerList:GetLine( PlayerList:GetSelectedLine() ):GetValue(1) )

This looks more promising.

ty i appreaciate it

[editline]24th December 2013[/editline]

can i just add u on steam i have a few more questions

My netbook I’m on doesn’t have steam on it right now, sorry. I’m in bed unable to sleep so I got on my laptop to pass time. Christmas for me is today, since my mom has to work tomorrow :stuck_out_tongue:

Ok. My steam is OMGOMG132 write it somewhere :stuck_out_tongue:

I will add you later, just know I am quite busy so I don’t always have time to stop and help all the time.
The wiki never sleeps though :slight_smile: