A useful lua player kicker

I recently (with a lot of help from the facepunch community) managed to create a useful lua to kick players. Seeing as it was this community that helped me so much, I figured that by sharing it, I could help someone who is searching for what I was and give something back to the community. With this lua, you bind “open_kick_menu” to a button and press it, this creates a pop-up window that you type the to-be-kicked player’s name into and press enter, kicking the player, much quicker than opening the console and typing “kick” yourself.
**If you are going to use this with source dedicated server, change the underlined and bold “kick” to “rcon kick” **

facepunch helpers:
zeppelin
TylerB
Undefined
Persious



concommand.Add("open_kick_menu", function()
    local ply = LocalPlayer()
 
    local BackGround = vgui.Create ( "DFrame" )
    BackGround:SetSize( 200, 70 )
    BackGround:SetPos( (ScrW()/2)-BackGround:GetWide(),(ScrH()/2)-BackGround:GetTall() )
    BackGround:SetTitle( "Kick Player" )
    BackGround:SetVisible( true )
    BackGround:SetDraggable( true )
    BackGround:ShowCloseButton ( true )
    BackGround:MakePopup()
    BackGround.Paint = function()
        draw.RoundedBox(4, 0, 0, BackGround:GetWide(), BackGround:GetTall(), Color(200,0,0,200))
        draw.RoundedBox(2, 2, 2, BackGround:GetWide()-4, 21, Color(50,50,50,200))
    end
 
    local TextEntry = vgui.Create( "DTextEntry", BackGround )
    TextEntry:SetPos( 20,30 )
    TextEntry:SetTall( 20 )
    TextEntry:SetWide( 160 )
    TextEntry:SetEnterAllowed( true )
    TextEntry.OnEnter = function()
        RunConsoleCommand("**kick**", TextEntry:GetValue()) 
        BackGround:SetVisible( false )
    end
end)


Thanks for the credit.

I would recommend using


 or [lua] tags.

Or open the chat menu and !kick <user> <reason> ?

Untested but this should work

[Lua]

local function FindPlayer( Name )

for k,v in pairs( player.GetAll() ) do
	if ( string.find( v:Name( ) , Name ) ) then
		return v
	end
end

return false

end

hook.Add( “PlayerSay” , “ChatCommand” , function( Pl, Text )

if ( not Pl:IsAdmin( ) ) then return end

for Name, Reason in string.match( Text , "!kick (%b\"\") (%b\"\")" ) do

	local Target = FindPlayer( Name )
	if Target then
	
		Target:Kick( Reason )
		
	end
	
end

end)
[/Lua]

Thats a custom plugin isn’t? Atleast the server I compiled this for doesn’t have that command. I’m sure that comes as part of the Evolve admin mod. This is a stand alone player kicker.

No it’s part of the chat system if I’m not mistaken? Maybe you have an outdated version of Evolve. And by the way ( because I’m pedantic ), You don’t “compile” lua.

yeah but i didn’t want to say that i made it becuase i didn’t, not by myself anyway… didn’t know what else to say lol

Just say programmed. It’s a nice release though, maybe you will become an excellent coder one day.

thanks, lol maybe one day…

No offence but I thought your code was ugly so I rewrote it in 10 lines.

[lua]hook.Add(“PlayerSay”,“ChatKick”, function(pl,txt)
if !pl:IsAdmin() then return end;

local name, reason = txt:match("!kick (%w-) (.+)") 
for a , b in pairs(player.GetAll()) do
	if b:Nick():lower():find(name:lower()) then
		b:Kick(reason);
		return;
	end
end

end )[/lua]

No offence but the code I posted wasn’t meant for people like you. I made the code in that way for a reason, overly verbose ,because I hadn’t tested it, and so if it didn’t work, at the very least people would understand with more ease what I was trying to do.

And on another point, while we are throwing stones, if you want your code to be even better you would at least optimize it to a minimum.

[lua]
local MyFunction,name,reason;
function MyFunction(pl,txt)
if !pl:IsAdmin() then return end

name, reason = txt:match("!kick (%w-) (.+)") 
for a , b in pairs(player.GetAll()) do
	if b:Nick():lower():find(name:lower()) then
		b:Kick(reason);
		return;
	end
end

end )

hook.Add(“PlayerSay”,“ChatKick”,MyFunction)[/lua]

And On another point, What would you do if 2 players had very similar names, including spaces? You code doesn’t allow the user to designate a player by his full name if they have spaces in it.

Name,reason should be local to the function not outside it (Forgot to do it myself the first time though)

There’s no need to declare MyFunction as local as it is already inherent by the hook.Add call. Storing another reference to it is not needed.

If you’re going to tell me to optimise my code, don’t make it worse while doing it :wink:

About the lack of spaces, autofill with quotes would solve that easily (is what I do in my admin mod)

[editline]3rd September 2012[/editline]

P.s. if you want to go balls deep on it might as well switch to ipairs too :stuck_out_tongue:

I don’t think you get what I’m doing, I’m localizing the function because lambda type syntax ( using an un-referenced function as an argument ) is 12 times slower that localizing it. As for the name and reason, I localized it outside the function because for some reason you thought it would be good to make it a global.

The Irony, IPairs is slower than pairs because it has to do it in order. You would know this if you had read the PiL

I simply forgot to type local in front of it. I haven’t touched lua in around 4 months until today; so I’m a little rusty.

Have you got a source for the lambda syntax being slower than passing a local reference? Theoretically while the call will be slower the runtime of the code will remain exactly the same.

ipairs is faster in sequential tables. You would know this if you knew lua at all. But hey, you’ve been trying to learn for around two or three years, maybe I should throw you a bone and encourage you to keep trying.

I’m not going to post it now, no, because I have a life to wake up to tomorrow, and therefore should get a minimum of sleep. But please, bye no means does that mean you can’t do your own tests. For someone that is “rusty”, you sure do like to pick up someone else’s code and belittle it. I would suggest you just accept that my reply wasn’t at all intended for you, and that it was so that the OP could learn from it, not so that he could use it.

Strong words from someone who hasn’t released anything. As for the tying part, I’ve got it nailed. Tell me next time you feel like writting your own language in Lua, or brainfuck interpreter and encoder, or administration mod, and all the other stuff I have released publicly on my portfolio, because I might be able to give you a hint on how to localize variables.

I singlehandedly started the lua anti-cheat encouragement in 2009, wrote the most complex anti-cheat known to date, released an early version of it, wrote my own modular admin-mod and numerous other things, hacked Lua into supporting function overloading and emulated quantum computing processing (qubits).

I must say I admire your ego. I’ve always thought I had the largest one around the lua scene.

Well by all means, don’t hold back on posting links. Meanwhile I’m going for a nap.

Hi I’m Flapadar. Enjoy your nap.

[lua]local t = {}
for i = 1, 100000, 1 do table.insert( t, 5 ) end

local last = SysTime()
for k, v in pairs(t) do
t[k] = v * 10000
end

print(SysTime()-last)
last = SysTime()

for k, v in ipairs(t) do
t[k] = v * 10000
end
print(SysTime()-last)[/lua]

0.015625
0.018554686999778

Am I missing something here?

Changing a table while iterating with ipairs might slow it down. When I ran in native lua both were exactly the same and I’m fairly certain it’s faster in luajit.

http://img707.imageshack.us/img707/4135/1000m.png

ipairs is fast for small sequential tables but slows as you have higher keys and non-sequential keys as shown by haza a while ago. for i= is still faster than both though.