Need some help with coding a weapon to TTT

IT IS DONE, THANKS TO ALL WHO GAVE ME THEIR SNIPPETS OF CODES <3


Changed my title to fit my current problem, scroll down please :slight_smile:
EDIT: Title doesn’t change, dangit. I’ve got a problem with my sound :open_mouth:


I’m a newbie in LUA, but I’m interested and willing to learn :3

Ok, so the thing is, I’ve got the overused lightsaber to work with TTT (force jumps and such inc.), and now I want the SWEP to play a custom sound (Star Wars music, obv.) when I press the reload button. Not hard? Ofcourse not, unless it bugs everything else you just did…
I simply did:



function SWEP:Think()
	if self.Owner:KeyDown( IN_RELOAD ) then
				self.Owner:EmitSound(Sound("imperialmarch.wav")))
		end
end


Now, there’s basically 2 problems.
1-The sound plays double, triple, quadruple, who knows how many times fastly after eachother, but it resets the whole song once you press RELOAD again. How to make it play only once?
2-This broke the right-click force jump, you can do it once and after you landed, you can’t even left click anymore.

The code for the jump is:


function SWEP:SecondaryAttack()
         if SERVER then self.Owner:EmitSound(Sound("ls/lightsaber_swing"..tostring(math.random(2,3))..".wav")) end
         if SERVER then self.Owner:EmitSound(Sound("player/suit_sprint.wav")) end
         
         self.Weapon:SetNextSecondaryFire(CurTime() + 100)
         self.Weapon:SetNextPrimaryFire(CurTime() + 100)

         self.PrevHP = self.Owner:Health()
         
         if SERVER then self.Owner:SetVelocity(self.Owner:GetForward() * 400 + Vector(0,0,400)) end
         
         timer.Simple( 0.2, function() self.SecondaryAttackDelay(self) end )
end


Perhaps the Think function collides with the other one already in there?

Ultimately: The song plays ONCE, and can be stopped by tapping RELOAD again. There are multiple songs which play randomly or can somehow be chosen.

Again, I’m a newbie and I am still learning the ropes. Even if that means hacking things together sometimes :expressionless:

Only thing I could guess with the jump is something is already using SWEP:Think and you replaced it with that new code.

But then I guess it wouldn’t work at all. Here it works once and then just block everything else from happening. :confused:

No errors in console either, so yea :expressionless:

Hmm… I dont know what to tell you man. I’ll look a little harder and tell you if I get the answer.

I won’t be answering soon, but here’s the whole code for investigation if anyone cares :stuck_out_tongue:
Just remember, cutting of the next part makes it work:


 function SWEP:Think()
	if self.Owner:KeyDown( IN_RELOAD ) then
				self.Owner:EmitSound(Sound("imperialmarch.wav")))
		end
end

FULL CODE (plz don’t rip it ;_:wink:



if ( SERVER ) then

	AddCSLuaFile( "shared.lua" )
	
	
end

SWEP.HoldType			= "melee"

if ( CLIENT ) then

	SWEP.PrintName			= "///'s TTT Lightsaber"			
	SWEP.Author				= "///"
	SWEP.Slot				= 1
	SWEP.SlotPos			= 0
	SWEP.IconLetter			= "u"
	SWEP.DrawCrosshair		= false
	
        killicon.AddFont( "weapon_knife", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) )


end

-----------------------Main functions----------------------------
 
-- function SWEP:Reload() --To do when reloading
-- end 
 
function SWEP:JumpReset()
         self.JumpRefire = true
end

function SWEP:Think()
	if self.Owner:KeyDown(IN_JUMP) then
		if self.Owner:IsOnGround() then
			self.Owner:SetVelocity(Vector(0,0,500))
		end
	end
	
         //Jump think
         if self.InAirDmg == 1 then
            local trace = {}
	    trace.start = self.Owner:GetPos()
	    trace.endpos = self.Owner:GetPos() + (self.Owner:GetUp() * -10)
	    trace.filter = self.Owner
	
	    local tr2 = util.TraceLine(trace)

	    self.Owner:SetHealth(self.PrevHP)
	    
	    if tr2.Hit then
	       self.InAirDmg = 0
	       self.JumpRefire = false
               self.Weapon:SetNextSecondaryFire(CurTime() + 0.8)
               self.Weapon:SetNextPrimaryFire(CurTime() + 0.3)

	       if SERVER then self.Owner:EmitSound(Sound("player/pl_pain5.wav")) end
            else
             local ang = self.Owner:GetAimVector()
	     local spos = self.Owner:GetShootPos()

	     local trace = {}
	     trace.start = spos
	     trace.endpos = spos + (ang * 150)
	     trace.filter = self.Owner
	
	     local tr = util.TraceLine(trace)
	 
	     if tr.HitNonWorld and self.JumpRefire == true then
	        self.JumpRefire = false

                local bullet = {}
                bullet.Num=5
                bullet.Src = self.Owner:GetShootPos()
	        bullet.Dir= self.Owner:GetAimVector()
	        bullet.Spread = Vector(0.1,0.1,0.1)
	        bullet.Tracer = 0
	        bullet.Force = 500
	        bullet.Damage = 200
	        self.Owner:FireBullets(bullet)
	        self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER)

	        
	        timer.Simple(0.5,self.JumpReset,self)
	        
                if SERVER then self.Owner:EmitSound(Sound("npc/vort/claw_swing"..tostring(math.random(1,2))..".wav")) end
                if SERVER then self.Owner:EmitSound(Sound("ls/lightsaber_swing"..tostring(math.random(1,3))..".wav")) end
				
             end
            end
           end
end

function SWEP:Think()
	if self.Owner:KeyDown( IN_RELOAD ) then
				self.Owner:EmitSound(Sound("imperialmarch.wav"))
		end
end

function SWEP:Initialize()
util.PrecacheSound("physics/flesh/flesh_impact_bullet" .. math.random( 3, 5 ) .. ".wav")
util.PrecacheSound("weapons/iceaxe/iceaxe_swing1.wav")
self:SetWeaponHoldType("melee") 
end
 
function SWEP:PrimaryAttack()
self.Weapon:SetNextPrimaryFire(CurTime() + .50)

local trace = self.Owner:GetEyeTrace()

if trace.HitPos:Distance(self.Owner:GetShootPos()) <= 75 then
self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER)
	bullet = {}
	bullet.Num    = 1
	bullet.Src    = self.Owner:GetShootPos()
	bullet.Dir    = self.Owner:GetAimVector()
	bullet.Spread = Vector(0, 0, 0)
	bullet.Tracer = 0
	bullet.Force  = 0
	bullet.Damage = 1000
self.Owner:FireBullets(bullet)
self.Owner:SetAnimation( PLAYER_ATTACK1 );
self.Weapon:EmitSound("ls/lightsaber_swing" .. math.random( 3, 5 ) .. ".wav")
else
	self.Weapon:EmitSound("ls/ls_throw")
	self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
	self.Owner:SetAnimation( PLAYER_ATTACK1 )
end

end

/*---------------------------------------------------------
		SecondaryAttack
---------------------------------------------------------*/
SWEP.InAirDmg = 0
function SWEP:SecondaryAttack()
         if SERVER then self.Owner:EmitSound(Sound("ls/lightsaber_swing"..tostring(math.random(2,3))..".wav")) end
         if SERVER then self.Owner:EmitSound(Sound("player/suit_sprint.wav")) end
         
         self.Weapon:SetNextSecondaryFire(CurTime() + 100)
         self.Weapon:SetNextPrimaryFire(CurTime() + 100)

         self.PrevHP = self.Owner:Health()
         
         if SERVER then self.Owner:SetVelocity(self.Owner:GetForward() * 400 + Vector(0,0,400)) end
         
         timer.Simple( 0.2, function() self.SecondaryAttackDelay(self) end )
end

function SWEP:SecondaryAttackDelay()
         self.InAirDmg = 1
         self.JumpRefire = true
end




-------------------------------------------------------------------

------------General Swep Info---------------
SWEP.Author   = "///"
SWEP.Contact        = ""
SWEP.Purpose        = ""
SWEP.Instructions   = ""
SWEP.Spawnable      = false
SWEP.AdminSpawnable  = true
SWEP.Base				= "weapon_tttbase"
SWEP.ViewModelFlip		= false
-----------------------------------------------

------------Models---------------------------
SWEP.ViewModel      = "models/weapons/v_crewbar.mdl"
SWEP.WorldModel   = "models/weapons/v_crewbar.mdl"
-----------------------------------------------

-------------Primary Fire Attributes----------------------------------------
SWEP.Primary.Delay			= 1000	--In seconds
SWEP.Primary.Recoil			= 0		--Gun Kick
SWEP.Primary.Damage			= 33	--Damage per Bullet
SWEP.Primary.NumShots		= 1		--Number of shots per one fire
SWEP.Primary.Cone			= 0 	--Bullet Spread
SWEP.Primary.ClipSize		= 0	--Use "-1 if there are no clips"
SWEP.Primary.DefaultClip	= 0	--Number of shots in next clip
SWEP.Primary.Automatic   	= false	--Pistol fire (false) or SMG fire (true)
SWEP.Primary.Ammo         	= "none"	--Ammo Type
-------------End Primary Fire Attributes------------------------------------
 
-------------Secondary Fire Attributes-------------------------------------
SWEP.Secondary.Delay		= 120
SWEP.Secondary.Recoil		= 0
SWEP.Secondary.Damage		= 0
SWEP.Secondary.NumShots		= 1
SWEP.Secondary.Cone			= 0
SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic   	= true
SWEP.Secondary.Ammo         = "none"
-------------End Secondary Fire Attributes--------------------------------
function SWEP:Deploy()
	self.Weapon:EmitSound( "weapons/knife/knife_deploy1.wav" ) --Plays a nifty sound when you pull it out.
end


See you DO have two SWEP:Think’s. You can NOT have to of the same functions…

Ok I’ll fix that tomorrow, but I remember putting it in and it still would bug out. We’ll see :3

Hur hur, many be thankings to Cereal thus far, I merged the two functions and both seem to be working most excellently!

Now, next problem is, the song seems to play multiple times simultaneously. If anyone knows how to make it play ONCE, with multiple songs (RANDOMLY or somehow selectable) and a way to kind of have a play/pause button or smth. I’ve also fixed a timer.Simple at the jumpreset.

Or perhaps some way to limit the emitted sounds to 1?

Please post the new code, and use lua tags. They are way easier to work with.

[editline]10th February 2013[/editline]

Try:
[lua]
self.Weapon:EmitSound(Sound(“imperialmarch.wav”))
[/lua]
Never really done something like this, but you’re really freindly and I’d like to help…

Aah k. I knew there was something like that, but I wasn’t entirely sure how (I tried the tags before and it didn’t work? :|)

Here it is:
[lua]
if ( SERVER ) then

AddCSLuaFile( "shared.lua" )

end

SWEP.HoldType = “melee2”

if ( CLIENT ) then

SWEP.PrintName			= "///'s STRONK Lightsaber"			
SWEP.Author				= "///"
SWEP.Slot				= 1
SWEP.SlotPos			= 0
SWEP.IconLetter			= "u"
SWEP.DrawCrosshair		= false

    killicon.AddFont( "weapon_knife", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) )

end

-----------------------Main functions----------------------------

– function SWEP:Reload() --To do when reloading
– end

function SWEP:JumpReset()
self.JumpRefire = true
end

function SWEP:Think()
if self.Owner:KeyDown(IN_JUMP) then
if self.Owner:IsOnGround() then
self.Owner:SetVelocity(Vector(0,0,500))
end
end

     //Jump think
     if self.InAirDmg == 1 then
        local trace = {}
    trace.start = self.Owner:GetPos()
    trace.endpos = self.Owner:GetPos() + (self.Owner:GetUp() * -10)
    trace.filter = self.Owner

    local tr2 = util.TraceLine(trace)

    self.Owner:SetHealth(self.PrevHP)
    
    if tr2.Hit then
       self.InAirDmg = 0
       self.JumpRefire = false
           self.Weapon:SetNextSecondaryFire(CurTime() + 0.8)
           self.Weapon:SetNextPrimaryFire(CurTime() + 0.3)

       if SERVER then self.Owner:EmitSound(Sound("player/pl_pain5.wav")) end
        else
         local ang = self.Owner:GetAimVector()
     local spos = self.Owner:GetShootPos()

     local trace = {}
     trace.start = spos
     trace.endpos = spos + (ang * 150)
     trace.filter = self.Owner

     local tr = util.TraceLine(trace)
 
     if tr.HitNonWorld and self.JumpRefire == true then
        self.JumpRefire = false

            local bullet = {}
            bullet.Num=5
            bullet.Src = self.Owner:GetShootPos()
        bullet.Dir = self.Owner:GetAimVector()
        bullet.Spread = Vector(0.1,0.1,0.1)
        bullet.Tracer = 0
        bullet.Force = 500
        bullet.Damage = 200
        self.Owner:FireBullets(bullet)
        self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER)

        
        timer.Simple(0.5, function () self.JumpReset(self) end )
        
            if SERVER then self.Owner:EmitSound(Sound("npc/vort/claw_swing"..tostring(math.random(1,2))..".wav")) end
            if SERVER then self.Owner:EmitSound(Sound("weapons/ls/lightsaber_swing"..tostring(math.random(1,5))..".wav")) end
			
         end
        end
       end
	   
if self.Owner:KeyDown( IN_RELOAD ) then
		if self.Owner:EmitSound(Sound("weapons/ls/starwarsmusic" .. math.random(1,2) .. ".wav")) then
		self.Owner:StopSound(Sound("weapons/ls/starwarsmusic" .. math.random(1,2) .. ".wav"))
		else
		self.Owner:EmitSound(Sound("weapons/ls/starwarsmusic" .. math.random(1,2) .. ".wav"))
	self.Weapon:SetNextPrimaryFire(CurTime() + 2)
	end	   
end

end

function SWEP:Initialize()
util.PrecacheSound(“physics/flesh/flesh_impact_bullet” … math.random( 3, 5 ) … “.wav”)
util.PrecacheSound(“weapons/iceaxe/iceaxe_swing1.wav”)
self:SetWeaponHoldType(“melee”)
end

function SWEP:PrimaryAttack()
self.Weapon:SetNextPrimaryFire(CurTime() + .50)

local trace = self.Owner:GetEyeTrace()

if trace.HitPos:Distance(self.Owner:GetShootPos()) <= 75 then
self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER)
bullet = {}
bullet.Num = 1
bullet.Src = self.Owner:GetShootPos()
bullet.Dir = self.Owner:GetAimVector()
bullet.Spread = Vector(0, 0, 0)
bullet.Tracer = 0
bullet.Force = 0
bullet.Damage = 1000
self.Owner:FireBullets(bullet)
self.Owner:SetAnimation( PLAYER_ATTACK1 );
self.Weapon:EmitSound(Sound(“weapons/ls/lightsaber_swing” … math.random( 1, 5 ) … “.wav”))
else
self.Weapon:EmitSound(Sound(“weapons/ls/ls_throw”))
self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
self.Owner:SetAnimation( PLAYER_ATTACK1 )
end

end

/---------------------------------------------------------
SecondaryAttack
---------------------------------------------------------
/

SWEP.InAirDmg = 0
function SWEP:SecondaryAttack()
if SERVER then self.Owner:EmitSound(Sound(“weapons/ls/lightsaber_swing”…tostring(math.random(1,5))…".wav")) end
if SERVER then self.Owner:EmitSound(Sound(“player/suit_sprint.wav”)) end

     self.Weapon:SetNextSecondaryFire(CurTime() + 100)
     self.Weapon:SetNextPrimaryFire(CurTime() + 100)

     self.PrevHP = self.Owner:Health()
     
     if SERVER then self.Owner:SetVelocity(self.Owner:GetForward() * 400 + Vector(0,0,400)) end
     
     timer.Simple( 0.2, function() self.SecondaryAttackDelay(self) end )

end

function SWEP:SecondaryAttackDelay()
self.InAirDmg = 1
self.JumpRefire = true
end


------------General Swep Info---------------
SWEP.Author = “///”
SWEP.Contact = “”
SWEP.Purpose = “”
SWEP.Instructions = “”
SWEP.Spawnable = false
SWEP.AdminSpawnable = true
SWEP.Base = “weapon_tttbase”
SWEP.ViewModelFlip = false

------------Models---------------------------
SWEP.ViewModel = “models/weapons/v_crewbar.mdl”
SWEP.WorldModel = “models/weapons/v_crewbar.mdl”

-------------Primary Fire Attributes----------------------------------------
SWEP.Primary.Delay = 1000 --In seconds
SWEP.Primary.Recoil = 0 --Gun Kick
SWEP.Primary.Damage = 5000 --Damage per Bullet
SWEP.Primary.NumShots = 1 --Number of shots per one fire
SWEP.Primary.Cone = 0 --Bullet Spread
SWEP.Primary.ClipSize = 0 --Use “-1 if there are no clips”
SWEP.Primary.DefaultClip = 0 --Number of shots in next clip
SWEP.Primary.Automatic = false --Pistol fire (false) or SMG fire (true)
SWEP.Primary.Ammo = “none” --Ammo Type
-------------End Primary Fire Attributes------------------------------------

-------------Secondary Fire Attributes-------------------------------------
SWEP.Secondary.Delay = 120
SWEP.Secondary.Recoil = 0
SWEP.Secondary.Damage = 0
SWEP.Secondary.NumShots = 1
SWEP.Secondary.Cone = 0
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = true
SWEP.Secondary.Ammo = “none”
-------------End Secondary Fire Attributes--------------------------------

function SWEP:Deploy()
self.Weapon:EmitSound(Sound( “weapons/ls/saberon.wav” )) --Plays a nifty sound when you pull it out.
end

–DISABLED, NOT ABLE TO HOLSTER-- function SWEP:Holster()
–DISABLED-- self.Weapon:EmitSound(Sound( “weapons/ls/saberoff.wav” ))
–DISABLED–end[/lua]

So yea. Basically I need some sort of curtime delay so that the key can only be pressed once. And a START/STOP button.

Anw, back to messing with my code :expressionless:

what exactly is your current problem

Well obviously the code of the sound is wrong, but I put it so that if I press the RELOAD key, a sound plays. Now, the current problem is that the sounds overlap, and I need some sort of curtime on the reload key so it only registers once.

Why aren’t you using SWEP:Reload() to perform the sound instead of the think hook?
[lua]local shouldPlay = true
function SWEP:Reload()
if shouldPlay then /play sound here/; shouldPlay = false
/create a timer for the length of the sound clip, at the end set shouldPlay to true/
end
end[/lua]

Aw shii- son! Didn’t think of that! I’m quite inexperienced in LUA but thanks for helping :smiley:

So thus far it’s going great! I’ve got the timer to work and register only a single push, and instead of adjusting the timer to the length of the sound, I rather use it to register a single tap. But without that piece of code I wouldn’t have made it. Thanks >> :smiley:

Now I just need a way to stop the sound of SWEP. One button to stop all the sounds, so I thought: [lua]if self.Owner:KeyDown( IN_USE ) then
self.Weapon:StopSound(Sound(“weapons/ls/starwarsmusic1.wav” ) )
self.Weapon:StopSound(Sound(“weapons/ls/starwarsmusic2.wav” ) )
end[/lua] would work, but obviously it didn’t.

Anyone got any idea? :3

Also, I’ve got the holster sound set up, but I can’t holster my weapon :frowning:
[lua]
function SWEP:Holster()
self.Weapon:EmitSound(Sound( “weapons/ls/saberoff.wav” ))
end[/lua]

Ok so I’ve decided to go a different way with my weapon. I only need to know how to make a sound stop playing when you holster the weapon OR when you die. The rest is all fixed :3