I need small help with LUA. Please help only if you are good at LUA.

Can anyone help me with LUA? I need simple melee weapon code on weapon_base base. I need SIMPLE melee weapon not on GDCW base or M9K base no on TTT Base but on default (weapon_base)! It needs to have already set damage, because i always had problem with setting damage. If i said something wrong, sorry, i am from Poland and i am bad at english.

O-ookay, i will try it.

[editline]27th February 2015[/editline]

Man, i mean MELEE WEAPON like crowbar, knife or anything like that, i am lua noob and i can’t change fists into crowbar.

Just inherit the weapon_base and change the hold type. That will make a simple melee weapon.

But it has fists animations. I need crowbar anims.

-snip, understood incorrectly-

I don’t understand you, i don’t understand internet! I just wanted weapon code, or even a attack function, i understand nothing from your reply. If you are experienced LUA scripter, making primary attack function wouldn’t be so hard or be waste of time.

Sorry that this took a while, had to test some of the trace stuff.

local swingsound = "whatever.wav"
local hitsound = "whatever2.wav"

function SWEP:PrimaryAttack()
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )	-- No spam pls
	self.Weapon:EmitSound( swingsound )
	timer.Simple( 0.2, function()	-- Gives a grace period between the swing and the actual hit
		if IsValid( self ) and IsValid( self.Owner ) then	-- Time has passed; always check for validity after a timer
			self:Hit()	-- Here's where the magic happens
	end )
	self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER ) -- Dummy animation for what the weapon does in third person, act enums here: http://wiki.garrysmod.com/page/Enums/ACT
	self.Owner:SetAnimation( PLAYER_ATTACK1 ) -- Animation for first person

function SWEP:Hit()	-- Separate function for readability and code cleanliness
	if SERVER then -- For multiplayer; especially without lag compensation, traces will differ from client to server resulting in hits that should(n't) have passed
		pos = self.Owner:GetShootPos()
		ang = self.Owner:GetAimVector()
		me = self.Owner
		local hit = {}	-- Sets up tracing table to create range and position for hitting the entity. Angle and vector values good ranges, but definitely are configurable
		hit.start = pos
		hit.endpos = pos + ( ang * 70 )
		hit.filter = me
		hit.mins = Vector( -5, -5, 0 )
		hit.maxs = Vector(5, 5, 5)
		local trace = util.TraceHull( hit )	-- Do not use clientside, serverside will provide the hull trace of the weapon hit
		if trace.Hit then	-- If the weapon hits an object
			targ = trace.Entity	-- What did the weapon hit?
			if targ:IsPlayer() or targ:IsNPC() then	-- If it hit a player or NPC (living entities) ...
				self.Owner:EmitSound( hitsound )					
				paininfo = DamageInfo()				-- Calculates damage dealt and how it will be handled
				paininfo:SetDamage( self.Primary.Damage )
				paininfo:SetDamageType( DMG_CLUB ) -- or DMG_SLASH if you're making some kind of sharp weapon
				paininfo:SetAttacker( me )	-- I merely attacked the man...
				paininfo:SetInflictor( self.Weapon )	-- The weapon is who killed him
				paininfo:SetDamageForce( trace.Normal * 10000 )	-- Scales up normal (perpendicular) vector of the hit to create a realistic force
				targ:ViewPunch( Angle( -10, -20, 0 ) )	-- Shakes the swing, hitting people is hard work (optional, comment this line out if you don't want the shake)

Thanks! I wanted reply like this! Now i can make my very creative melee weapon! :smiley: :smiley:

[editline]27th February 2015[/editline]

But… same problem! weapon doesn’t take any damage!

[editline]27th February 2015[/editline]

I would write .exe file with notepad easier than setting damage for lua melee weapon!!!

Gee, do you want a cookie or something?

Did you set the SWEP.Primary.Damage variable?

Sure. I also had the same problem with my other abandoned SWEPs.

lol why’d you reply to me?

Post your whole file in


[editline]28th February 2015[/editline]

[quote="eligrand1, post:13, topic:198947"]

lol why'd you reply to me?

Wrong button