Running a command on the player you hit

Right now I am looking to run a command on the player you hit with a melee weapon. I just need to know how to target the person.
Here’s my primary attack code:


function SWEP:PrimaryAttack()

self.Weapon:EmitSound("weapons/hammer/swing" .. math.random( 1, 2 ) .. ".wav")
self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)

self.Owner:ViewPunch(Angle( -2, -3, 0 ))

local trace = {}
		trace.start = self.Owner:GetShootPos()
		trace.endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * 24^14
		trace.filter = self.Owner
	local tr = util.TraceLine(trace)

    tr.Entity:SetKeyValue("targetname", "disTarg")
	local dis = ents.Create("env_entity_dissolver")
	dis:SetKeyValue("magnitude", "5")
	dis:SetKeyValue("dissolvetype", "3")
	dis:SetKeyValue("target", "disTarg")
	dis:Spawn()
	dis:Fire("Dissolve", "disTarg", 1)
	dis:Fire("kill", "", 1)

if tr.HitPos:Distance(self.Owner:GetShootPos()) <= 95 then
	bullet = {}
	bullet.Num    = 1
	bullet.Src    = self.Owner:GetShootPos()
	bullet.Dir    = self.Owner:GetAimVector()
	bullet.Spread = Vector(0, 0, 0)    --0,0,0
	bullet.Tracer = 1
	bullet.Force  = 0
	bullet.Damage = 1
	bullet.AmmoType = "AR2Altfire"
local hit1, hit2 = tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal
self.Owner:SetAnimation( PLAYER_ATTACK1 );	
	local randomsounds = {
  "weapons/hammer/hammer_impact1.wav",
  "weapons/hammer/hammer_impact2.wav",
  "weapons/hammer/hammer_impact3.wav",
  "weapons/hammer/hammer_impact4.wav",
  "weapons/hammer/hammer_impact5.wav",
  "weapons/hammer/hammer_impact6.wav",
}
    local random = math.random(1, #randomsounds)
    self.Owner:EmitSound(randomsounds[random])
	util.Decal("FadingScorch", hit1, hit2)
	util.ScreenShake( Vector(0,0,0), 5, 5, 0.6, 5000 )
	self.Owner:ViewPunch(Angle( -5, -3, 4 ))
	
	local effect = EffectData()
	effect:SetOrigin(tr.HitPos)
	effect:SetNormal(tr.HitNormal)
	effect:SetScale(10)
	util.Effect("StunstickImpact", effect)
	self:ShootEffects()
	self.Owner:FireBullets( bullet )
	
    self.Owner:FireBullets(bullet)
	self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
	self.Owner:SetAnimation( PLAYER_ATTACK1 )
else
	self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
	self.Owner:SetAnimation( PLAYER_ATTACK1 )
end

end

Here is my (so far) command code:


function SWEP:PrimaryFire() // I am looking for a way to find the target when it kills someone on primary fire
game.ConsoleCommand("ev ban" target "5 The Ban Hammer Has Spoken!") // target = the player found above 
end


If someone could fix up my sloppy command code and help me out, that’d be great. I’m just trying to make a working ban hammer for fun :wink:

A couple things here…

I’m guessing this is for banning people via some obscure administration script. If there’s any way to avoid running console commands, I would try try that first.

game.ConsoleCommand() requires a single string (followed by "
" for enter, I believe). What you have is an un-concatenated argument where each word is interpreted as a separate argument.

Assuming you target variable returns a string-based ID of some sort, all you need to do is re-write your code to say:


game.ConsoleCommand("ev ban " .. target .. " 5 \"The Ban Hammer has spoken!\"
")

So this is assuming “ev” is the base command, “ban” is the primary argument, “target” is the ID of a player to ban, “5” is the duration and the following string is the reason or whatever.

There is a ban function in gmod by default.

Indeed.

Has it not occurred to you that maybe he’s not wanting to have a default ban system? Hence his command for banning starts with “ev”?

It never ceases to amuse me how unhelpful people here are to each other. It’s like so many of you think it’s just a dick measuring contest, and so by giving an answer that doesn’t really address the problem, you have therefore asserted your superior intellect.

Yeah, dude, we get it. There’s a default ban feature, there’s also a million other ways to address the same problem. Just because someone takes a different approach doesn’t make it any better than yours. This whole God-forsaken category is apparently about trying to one-up each other, not help people learn.

It’s like, “I don’t actually know how to fix anything, but this has the word ‘ban’ in it, and I am a smarter person, so I’ll post something completely unhelpful like it’s common sense and this guy’s retarded.”

Digressing, there are several ways the default ban system could be improved - which is exactly why so many different admin-scripts exist.

hey iSchmal, calling console commands to do stuff is dumb, Neth’s solution is slightly better, however the OP should be using the function evolve:Ban( uid, length, reason, adminuid ) ideally.

Oh, no kidding?

(granted, I did have a typo)

My point is, half the people on this forum are ignorant, self-entitled idiots who don’t actually know how to solve anything.

Also, the title explicitly begins with “Running a command…”, not “banning a player.” Why not let him do whatever he wants and actually answer his question?

Try working with


eyetrace.HitPos

And then run a command that you made like :

[LUA]function myCommand( ply, text, public)
if string.sub( text, 1, 6) == “!ulxban” ) then
ply:Ban( 1440, “Goodbye” )
end
end[/LUA]

Then just make upon eyetrace.HitPos he runs this

Why not just use

Make the ban hammer do damage, setup an inflictor or some other value inside the CTakeDamageInfo

Hook damage event, check for the ban hammer. If it is the ban hammer then ban them.

Traces and melee weapons seem… not the same. To me anyway. This way it only fires if you actually damage them with the hammer, which should play a cool sound. And advertise in chat who got banhammered. There should be a confetti effect, and their body should explode.