TTT Stun Baton SWEP

Howdy all. I’m attempting to make a Stun Baton SWEP for Detectives. So far it seems to function without any errors except that the attack animation is very glitchy. Half the time it doesn’t animate at all and a lot of the time the animation is too fast or erratic.


if SERVER then
	AddCSLuaFile("shared.lua")
end
 
if CLIENT then --Client stuff
	SWEP.PrintName = "Stun Baton" --Name
	SWEP.Slot = 7
	SWEP.DrawAmmo = false --Draw ammo in bottom right corner
	SWEP.DrawCrosshair = false --Draw crosshair?
end

SWEP.Base				= "weapon_tttbase"

SWEP.ViewModelFOV = 62
SWEP.ViewModelFlip = false
SWEP.AnimPrefix = "stunstick"
 
SWEP.Spawnable = false --Not spawnable by non-admin players
SWEP.AdminSpawnable = true --Only spawnable by admins!
 
SWEP.NextStrike = 0
 
SWEP.ViewModel = Model("models/weapons/v_stunstick.mdl") --The model while being held
SWEP.WorldModel = Model("models/weapons/w_stunbaton.mdl") --World model (on the ground)
 
SWEP.Sound = Sound("weapons/stunstick/stunstick_swing1.wav") --The sound
 
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = 0
SWEP.Primary.Automatic = false 
SWEP.Primary.Ammo = ""
 
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = 0
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = ""
 
function SWEP:Initialize()
	if SERVER then self:SetWeaponHoldType("normal") end --Set the hold type
end
 
function SWEP:PrimaryAttack()
	if CurTime() < self.NextStrike then return end
 
	if SERVER then
		self:SetWeaponHoldType("melee")
		-- timer.Simple(0.3, function(wep) if wep:IsValid() then wep:SetWeaponHoldType("normal") end end, self) --Checks that the swep hasn't quickly been switched
	end
	self.Owner:SetAnimation(PLAYER_ATTACK1)
	self.Weapon:EmitSound(self.Sound) ---Emit a sound, the stunstick wack
	self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER)
 
	self.NextStrike = CurTime() + .4
 
end
 
function SWEP:SecondaryAttack()
	self:PrimaryAttack() --Run Primary Attack
end

I made this by modifying the Darkrp version from thistutorial.

On every DarkRP server I played the stunstick animations were laggy as well, I’m not sure if that’s a problem with me or DarkRP though.

Ok well I guess I can live with that. However I’ve run into a new issue playing a random sound:


local soundnumber = math.Random(1,3)
	if soundnumber == 1 then
		self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan1.wav")
	elseif soundnumber == 2 then
		self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan2.wav")
	else
		self.Weapon:EmitSound("npc/metropolice/vo/pickupthecan3.wav")
	end

[editline]21st October 2013[/editline]

I was able to fix it by using ‘rand’ instead of ‘random’ however it always plays the same sound.

Where are you using that code? As in, what function

Inside SWEP:SecondaryAttack()

Try using ACT_VM_MISSCENTER instead of ACT_VM_HITCENTER, I don’t know why but HITCENTER seems buggy. It’s why the knife in TTT plays no animation when you attack with it and hit someone.

Lua is case sensitive, use “math.random” not “math.Random” also math.Rand returns a float, so it will never be 1 or 2.