How to ban a target

I’m making a Banhammer and I would like to know how do I ban the player?
How do I ban his IP and his alone only.

You are going to want to use this to get the player which the person is targeting with the hammer.
Player:GetEyeTrace()

Then use this on the resulting player data to get their IP address.
Player:IPAddress()

Not sure why you would want to ban his IP. They can easilly be changed or spoofed. I would suggest simply banning the persons account as that will ban there steamID which can’t be changed as easily as an IP. Especially since the player will have to be in game for it to work giving no real reason to ban their IP.

For a simple ban you would just need to use this.
Player:Ban()

Oh wow. This is a near perfect reply. Thanks!

[editline]21st October 2015[/editline]

But how do I make it so when the hammer hits him, he gets banned?
Can I use a hook where if he takes dmg from my hammer, he gets banned.
Is there a better and more refined way of doing this?

I personally would try doing the ban function in the weapon’s SWEP:PrimaryAttack() function.

Could you give me an example? Like a template I can use so I can apply it?

No offense man but you are basically asking us to do the work for you. We provided you with some tools now you need to go and build this on your own. You wont learn anything by us doing it for you. We will be happy to answer any questions but you need to make an attempt first.

No offense but I did.


HookyFunction = local if ( target:IsPlayer() and DamageInfo()IsDamageType(1, DMG:Club) ) then steamidmachine
end
hook.Add("EntityTakeDamage", "My banhammer hook", HookyFunction)
end
if "my banhammer hook" = true then Player:Ban(60)
end
if "my banhammer hook" = false then print "Banhammer is broken. Just use /ban for now and email me at pirateemoreg@gmail.com"
end

I want to refine this code and not use damage as a way to ban the guy.

If you need a template you can probably find countless with a simple google. You can also just take any addon with a melee weapon and extract it and look at that. If you even already have the hammer weapon but want it to ban, you can just modify it’s code to ban instead of damage.

It would have been helpful if you showed us you already had some kind of code. I am not sure what you are trying to do with that code but it isn’t going to work at all. You didn’t use the things I showed you properly at all. First off do you have this inside a SWEP or an autorun file?

Edit: Here is a tutorial on creating a SWEP I suggest you take a look.

I know how to create a SWEP.
Its the part where my hammer needs to hit the guy and ban him.
Thats the part where im having trouble.

I doubt that becuase its obvious you don’t know how to create a function or use a hook. You did both completely wrong, take a look at what cynaraos said that is where you code for the ban should be not inside a hook.

I didn’t come here to have someone tell me what I don’t know. I just started Lua 2 days ago. Although your criticism and feedback is important for me to learn. I would appreciate it if you could correct me and show me whats the right way to do it.

[editline]21st October 2015[/editline]

I would personally appreciate it if you could explain to me how to use a hook and function properly, considering I read tutorials about this online, it would be great if you could explain it to me fully.
Thanks

You screwed up arguably the most basic part of Lua I am not going to hold your hand and teach you the basics thats not what this section is for. There are plenty of tutorials to teach you hooks and functions. I already linked you to 1 tutorial about making a SWEP and you dismissed it claiming to already know how to make a SWEP.

You can find useful tutorials here.
I will be happy to help you with any questions you have regarding the SWEP but I am not going to write the code for your or teach you things that can be found in a tutorial.

I got my tutorials from maurits.tv because its more in-depth and i can understand it better.
Is the info from there outdated?

Clearly you didn’t read them properly since it seems you have no idea how conditionals, variables and functions work, the most basic things. maurits.tv is good but is slightly outdated though.

[sp]I was going to suggest how to fix your code but that is the Frankenstein’s monster of copypasting, seriously learn the basics first.[/sp]

Do you already have the weapon/template file of the ban hammer that you are modifying? In that case just put
[lua]ply:Ban(60, true)[/lua]
wherever the damage is otherwise performed, removing the damage causing code.

If you do not have the file of the weapon or you are trying to make it work externally from the weapon, you could use the GM:EntityTakeDamage hook. It passes a damage info object which you can call GetInflictor() (for the weapon) and GetAttacker() (for the player hitting with the hammer) on. Check if the inflictor is a weapon and if it’s class is that of your hammer, and check if the attacker is a player and is an admin. Might also wanna make sure to check if the victim entity is a valid player.

That will not work ply will be nil. You need to get the player the person is looking at hence the trace function. There is no need to use the damage hook this can all be done within SWEP:PrimaryAttack()

How do you get the data from the eye trace?

This dude literally told you exactly what to do and how to do it.

The trace will return a table of data and you can select which elements you want by using the documented structure on the wiki. You can find that page here.

Essentially you will use something like this.



local ent = self:GetOwner():GetEyeTrace().Entity -- Retrieving the entity hit by the trace


This will set the variable ent to the entity that the trace hits. Since players are considered entities you want to use the entity that is hit by the trace. Now players are not the only types of entities so to make sure what you are looking at is a player you need to add a IsPlayer() check to the trace.

So now it should look something like this.



local ent = self:GetOwner():GetEyeTrace().Entity ) -- Retrieving the entity hit by the trace
if (ent:IsPlayer() == true) then local target = ent end -- Checking that the entity is a player


The player who was hit by the hammer is now stored inside of the target variable so you now know who was hit. You can then use the ban function on this player. Hopefully this helps you a little bit. I really wish you would have made more of an attempt but I can see form your first attempt that you really could use some help. The rest is now up to you. I am not going to write anymore code for you until I see that you actually have put some effort in. It’s alright if you have problems or get errors I will be perfectly happy to assist you but only once you come back with an actual attempt at this.