Error with v:Kick()

So I have this code:
[lua]

function kick:DoClick()
			local kickmenu = DermaMenu()
			
			for k, v in pairs( player.GetAll() ) do
				
				kickmenu:AddOption( v:Nick(), function( ply ) v:Kick( "You have been kicked by" ) end )
				
			end
			kickmenu:Open()
			
		end

[/lua]

And I’m getting this error:
[lua]
[ERROR] addons/nadmin/lua/autorun/help_panel.lua:61: attempt to call method ‘Kick’ (a nil value)

  1. DoClick - addons/nadmin/lua/autorun/help_panel.lua:61
  2. OnMouseReleased - lua/vgui/dlabel.lua:204
    3. unknown - lua/vgui/dmenuoption.lua:116

[/lua]
How can I fix this? Thank you.

Player:Kick() is serverside not clientside.
http://wiki.garrysmod.com/page/Player/Kick <- Look in the bottom of the page (Server).

You can use the Net Library to network it to the server:

Oh Shit. I have been coding my admin menu today whole day that I forgot which file I was in xD Thanks CodingBeast. I watch your videos :slight_smile:

[editline]27th March 2015[/editline]

I have this:

[lua]
Clientside:

function kick:DoClick()
			local kickmenu = DermaMenu()
			
			for k, v in pairs( player.GetAll() ) do
				
				
					kickmenu:AddOption( v:Nick(), net.Receive( "kickplayer", function( v ) v:Kick( "You have been kicked!" ) end) )
				
			end
			kickmenu:Open()
			
		end

Serverside:
util.AddNetworkString( “kickplayer” )
function kickplayer()
net.Start( “kickplayer” )
net.Broadcast()
end

[/lua]

It returns no error but doesn’t kick

[editline]27th March 2015[/editline]

I also have this:

[lua]
[ERROR] addons/nadmin/lua/autorun/init.lua:8: attempt to call field ‘AddNetworkString’ (a nil value)

  1. unknown - addons/nadmin/lua/autorun/init.lua:8

[/lua]

You should probably drink some coffee because you got that the other way around, on the client you should send the message to kick the player, and on the server you should be receiving.

Also clientside it should be net.SendToServer() instead of broadcast.

Also you’re kicking v where you should be kicking the result from net.ReadEntity()

This error again:
[lua]
[ERROR] addons/nadmin/lua/autorun/help_panel.lua:62: attempt to call field ‘AddNetworkString’ (a nil value)

  1. DoClick - addons/nadmin/lua/autorun/help_panel.lua:62
  2. unknown - lua/vgui/dlabel.lua:204

[/lua]

AddNetworkString is serverside

You have to use that serverside at the start of the file.

[editline]27th March 2015[/editline]

Also it seems your init.lua is also getting called clientside because you put it in autorun, you may want to fix that and instead adding it to autorun/server.

Yeah but I’m using if (SERVER) then, if (CLIENT) then

[editline]27th March 2015[/editline]

If AddNetworkString is serverside and I put it in serverside then why is it still nil?

Clientside:
[lua]
if CLIENT then
function kick:DoClick()
local kickmenu = DermaMenu()
for k, v in pairs( player.GetAll() ) do
kickmenu:AddOption( v:Nick(), function()
net.Start(“kickplayer”)
net.WriteEntity(v)
net.SendToServer()
end )
end
kickmenu:Open()
end
end
[/lua]

Serverside:
[lua]
if SERVER then
util.AddNetworkString(“kickplayer”)
function kickplayer()
local ply = net.ReadEntity()
ply:Kick(“You have been kicked!”)
end
net.Receive(“kickplayer”, kickplayer)
end
[/lua]

Untested but should work.

Thank you very much CodingBeast. You’re the best man. I have been working on this for hours… Finally starting working on the actual commands and this will come in handy a lot :wink:

As a side note, if you have ULX or some equivalent administration system, you may just want to run a console command on LocalPlayer with whatever you want to do. For example if I wanted to run a kick command in ULX, I would do:



LocalPlayer():ConCommand('ulx kick $' .. v:SteamID() .. ' Hax')


This would allow only people who have the permission to kick, as opposed to anyone who opens the menu. Also, it would still print to everyone’s chatbox with the kicker and the target afterwards.

Why would you suggest anyone run two admin mods unless necessary?

Allow any client to kick anyone.
Genius mate.

It was just an example of how he could use the net library not something he should use in production.
And i would expect that he know how to do the check himself.

I wasn’t quite sure what he was doing with this. If this is to be a standalone addon, then, of course, don’t use two. If he is just creating another menu or something to be used with ULX or another system, then running a console command would work fine. If he he’s doing a standalone addon, it may be a better idea to make a console command in the first place (as that’s what’s usually wanted in any admin system) and run that on the LocalPlayer. Of course, using the net library will also work fine, but using a console command would be easier.