I made my own set of HL2 weapons based off Garry's Counter-strike SWEPs, but the reload sound wasn't working. So I added a sound in the reload function.
The problem now is, the sound plays whenever you press the reload key, even if the weapon doesn't need to be reloaded. How would I fix this so that the sound will only play when the weapon reloading animation plays/needs to be reloaded?
[code]local ReloadSound = Sound( "Weapon_Pistol.Reload" )
function SWEP:Reload()
self.Weapon:DefaultReload( ACT_VM_RELOAD )
self.Weapon:EmitSound( ReloadSound )
if
end[/code]
you could do an if than statement to check if the clip is full and if its not then play the sound
How would I do this, exactly? :/
[lua]if( !self.Weapon:Clip1() == Number here ) then
self.Weapon:DefaultReload( ACT_VM_RELOAD )
self.Weapon:EmitSound( ReloadSound )
else
return false
end[/lua]
This is untested
[url]http://wiki.garrysmod.com/?title=Weapon.Clip1[/url]
This doesn't let me reload at all, I've tried setting the "Number Here" to 0, and "0" but neither seem to work.
Dont use quotation marks and my bad i should have cleared it up try to set the number to the max clip size
There doesn't seem to be any change, it still doesn't let me reload. :/
This is correct?
[code]
function SWEP:Reload()
if( !self.Weapon:Clip1() == 18 ) then
self.Weapon:DefaultReload( ACT_VM_RELOAD )
self.Weapon:EmitSound( ReloadSound )
else
return false
end
end[/code]
[QUOTE=Laxsith;16814942]There doesn't seem to be any change, it still doesn't let me reload. :/[/QUOTE]
[code]
function SWEP:Reload()
if( self.Weapon:Clip1() < 18 ) then
self.Weapon:DefaultReload( ACT_VM_RELOAD )
self.Weapon:EmitSound( ReloadSound )
else
return false
end
end[/code]
try that, would you mind posting the swep code? Also do all of the sweps not work or just one?
This problem is only for the SMG and pistol.
Also, it worked! Thank you so much for helping me! :D
Success! First time I've helped someone out with their question instead of them helping me!:dance:
The code above also doesn't account for the possibility of the user running out of additional ammo.
DefaultReload returns true if the reload animation was played. So you could just do:
[code]
function SWEP:Reload()
if (self.Weapon:DefaultReload(VM_ACT_RELOAD)) then
self.Weapon:EmitSound( ReloadSound )
end
end
[/code]
[QUOTE=Airslide;16820948]The code above also doesn't account for the possibility of the user running out of additional ammo.
DefaultReload returns true if the reload animation was played. So you could just do:
[code]
function SWEP:Reload()
if (self.Weapon:DefaultReload(VM_ACT_RELOAD)) then
self.Weapon:EmitSound( ReloadSound )
end
end
[/code][/QUOTE]
This keeps the original problem I had though...
[lua]
--This sets the sound to whatever sound you want, you can even use loops, just be sure to stop them
function SWEP:Initialize()
self.ReloadSound = CreateSound(self.Weapon, "YOUR RELOADSOUND HERE")
--Other stuff here
end
--This plays the sound whenever you're reloading
function SWEP:Reload()
self.Weapon:DefaultReload( ACT_VM_RELOAD )
self.ReloadSound:Play()
end
--When holstering you stop reloading, This will stop the reloadsound
function SWEP:Holster()
self.ReloadSound:Stop();
return true
end[/lua]
I use something similar in my SWEPs, just be sure to add self.ReloadSound:Stop() to all functions you want it to stop ofc.
This will give an (invisible though) console error, if you have captions on, I don't think you can do anything to stop that
[b]EDIT: Oops... I solved the wrong problem D:[/b]
This is what you want:
[lua]
if ( self.Weapon:Ammo1() <= 0 ) then return end
if ( self.Weapon:Clip1() >= self.Primary.ClipSize ) then return end
[/lua]
Just put it into the reload function at the top
Sorry, you need to Log In to post a reply to this thread.