• Making an SWEP play music
    3 replies, posted
Hello there, I really need some help with coding my first SWEP. I followed the instructions on the gmod wiki for the "Chair gun" but found myself wanting to make the gun play some music when pressing the shoot button like the nyan cat gun. This is my script so far: [CODE]SWEP.PrintName = "Turtle Gun" SWEP.Author = "( QuiGon)" SWEP.Instructions = "Left Click To Shoot Freaking TURTLES!" SWEP.Spawnable = true SWEP.AdminOnly = false SWEP.Primary.ClipSize = -1 SWEP.Primary.DefaultClip = -1 SWEP.Primary.Automatic = true SWEP.Primary.Ammo = "none" SWEP.Secondary.ClipSize = -1 SWEP.Secondary.DefaultClip = -1 SWEP.Secondary.Automatic = false SWEP.Secondary.Ammo = "none" SWEP.Weight = 5 SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false SWEP.Slot = 1 SWEP.SlotPos = 2 SWEP.DrawAmmo = false SWEP.DrawCrosshair = true SWEP.ViewModel = "models/weapons/v_smg1.mdl" SWEP.WorldModel = "models/weapons/w_smg1.mdl" local ShootSound = Sound( "Metal.SawbladeStick" ) -- -- Called when the left mouse button is pressed -- function SWEP:PrimaryAttack() -- This weapon is 'automatic'. This function call below defines -- the rate of fire. Here we set it to shoot every 0.5 seconds. self.Weapon:SetNextPrimaryFire( CurTime() + 0.1 ) -- Call 'ThrowChair' on self with this model self:ThrowTurtle( "models/props/de_tides/vending_turtle.mdl" ) self.LoopSound = CreateSound( self.Owner, Sound( "weapons/Turtle.mp3" ) ) if ( self.LoopSound ) then self.LoopSound:Play() end end -- -- Called when the rightmouse button is pressed -- function SWEP:SecondaryAttack() -- Note we don't call SetNextSecondaryFire here because it's not -- automatic and so we let them fire as fast as they can click. -- Call 'ThrowChair' on self with this model self:ThrowTurtle( "models/props/de_tides/vending_turtle.mdl" ) end -- -- A custom function we added. When you call this the player will fire a chair! -- function SWEP:ThrowTurtle( model_file ) -- -- Play the shoot sound we precached earlier! -- -- -- If we're the client ) then this is as much as we want to do. -- We play the sound above on the client due to prediction. -- ( if ( we didn't they would feel a ping delay during multiplayer ) -- if ( CLIENT ) then return end -- -- Create a prop_physics entity -- local ent = ents.Create( "prop_physics" ) -- -- Always make sure that created entities are actually created! -- if ( !IsValid( ent ) ) then return end -- -- Set the entity's model to the passed in model -- ent:SetModel( model_file ) -- -- Set the position to the player's eye position plus 16 units forward. -- Set the angles to the player'e eye angles. Then spawn it. -- ent:SetPos( self.Owner:EyePos() + ( self.Owner:GetAimVector() * 16 ) ) ent:SetAngles( self.Owner:EyeAngles() ) ent:Spawn() -- -- Now get the physics object. Whenever we get a physics object -- we need to test to make sure its valid before using it. -- If it isn't ) then we'll remove the entity. -- local phys = ent:GetPhysicsObject() if ( !IsValid( phys ) ) then ent:Remove() return end -- -- Now we apply the force - so the chair actually throws instead -- of just falling to the ground. You can play with this value here -- to adjust how fast we throw it. -- local velocity = self.Owner:GetAimVector() velocity = velocity * 1000 velocity = velocity + ( VectorRand() * 50 ) -- a random element phys:ApplyForceCenter( velocity ) -- -- Assuming we're playing in Sandbox mode we want to add this -- entity to the cleanup and undo lists. This is done like so. -- cleanup.Add( self.Owner, "props", ent ) undo.Create( "Thrown_Chair" ) undo.AddEntity( ent ) undo.SetPlayer( self.Owner ) undo.Finish() end[/CODE] Yes I left in the comments but I was too lazy to take 'em out :P. What code do I enter for it to play music? And also I don't know where to put specific code so if you could include that in your post that'd be awesome!
If you are trying to make it play music like with the nyancat gun, two things, one is to make it auto-download the music so it works plays music instead of giving errors, two is you need to loop it with the gun fire like with the nyan cat gun, you can always check the script for the nyan cat gun for help :) [CODE] if ( SERVER ) then AddCSLuaFile() resource.AddFile("sound/weapons/nyan/nya2.mp3") resource.AddFile("sound/weapons/nyan/nyan_loop.mp3") resource.AddFile("sound/weapons/nyan/nyan_beat.mp3") resource.AddFile("sound/weapons/nyan/nya1.mp3") resource.AddFile("materials/nyan/killicon2.png") resource.AddFile("materials/nyan/cat_reversed.png") resource.AddFile("materials/nyan/rainbow.vmt") resource.AddFile("materials/nyan/killicon.vtf") resource.AddFile("materials/nyan/killicon.png") resource.AddFile("materials/nyan/selection.png") resource.AddFile("materials/nyan/rainbow.vtf") resource.AddFile("materials/nyan/killicon_bomb.vmt") resource.AddFile("materials/nyan/rainbow.png") resource.AddFile("materials/nyan/cat.vtf") resource.AddFile("materials/nyan/killicon_bomb.vtf") resource.AddFile("materials/nyan/killicon.vmt") resource.AddFile("materials/nyan/cat.vmt") resource.AddFile("materials/nyan/cat.png") resource.AddFile("materials/entities/weapon_nyangun.png") end if ( CLIENT ) then SWEP.SlotPos = 5 killicon.Add( "weapon_nyangun", "nyan/killicon", color_white ) SWEP.WepSelectIcon = Material( "nyan/selection.png" ) SWEP.BounceWeaponIcon = false SWEP.DrawWeaponInfoBox = false end SWEP.EquipMenuData = { name = "NyanGun", type = "item_weapon", desc = "Shoots Nyan Cats" }; SWEP.Slot = 7 SWEP.Kind = WEAPON_EQUIP SWEP.CanBuy = {ROLE_DETECTIVE, ROLE_TRAITOR} SWEP.LimitedStock = false SWEP.Icon = "entities/weapon_nyangun.png" SWEP.Base = "weapon_tttbase" SWEP.PrintName = "Nyan Gun" SWEP.Category = "Robotboy655's Weapons" SWEP.ViewModel = "models/weapons/v_smg1.mdl" SWEP.WorldModel = "models/weapons/w_smg1.mdl" SWEP.Spawnable = true SWEP.AdminSpawnable = true SWEP.AutoSpawnable = false SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false SWEP.HoldType = "smg" SWEP.ViewModelFlip = false SWEP.Primary.ClipSize = 1 SWEP.Primary.Delay = 0.1 SWEP.Primary.DefaultClip = 1 SWEP.Primary.Automatic = true SWEP.Primary.Ammo = "none" SWEP.Secondary.ClipSize = 1 SWEP.Secondary.Delay = 0.5 SWEP.Secondary.DefaultClip = 1 SWEP.Secondary.Automatic = false SWEP.Secondary.Ammo = "none" function SWEP:Initialize() self:SetWeaponHoldType( self.HoldType ) end function SWEP:DrawWeaponSelection( x, y, wide, tall, alpha ) surface.SetDrawColor( 255, 255, 255, alpha ) surface.SetMaterial( self.WepSelectIcon ) surface.DrawTexturedRect( x + 10, y, 128, 128 ) end function SWEP:PrimaryAttack() if ( !self:CanPrimaryAttack() ) then return end if (self.Owner:IsNPC()) then self:EmitSound( "weapons/nyan/nya" .. math.random( 1, 2 ) .. ".mp3", 100, math.random( 60, 80 ) ) else if ( self.LoopSound ) then self.LoopSound:ChangeVolume( 1, 0.1 ) else self.LoopSound = CreateSound( self.Owner, Sound( "weapons/nyan/nyan_loop.mp3" ) ) if ( self.LoopSound ) then self.LoopSound:Play() end end if ( self.BeatSound ) then self.BeatSound:ChangeVolume( 0, 0.1 ) end end local bullet = {} bullet.Num = 1 bullet.Src = self.Owner:GetShootPos() bullet.Dir = self.Owner:GetAimVector() bullet.Spread = Vector( 0.01, 0.01, 0 ) bullet.Tracer = 1 bullet.Force = 5 bullet.Damage = 9 //bullet.AmmoType = "Ar2AltFire" -- For some extremely stupid reason this breaks the tracer effect bullet.TracerName = "rb655_nyan_tracer" self.Owner:FireBullets( bullet ) self:SendWeaponAnim( ACT_VM_PRIMARYATTACK ) self.Owner:SetAnimation( PLAYER_ATTACK1 ) self:SetNextPrimaryFire( CurTime() + self.Primary.Delay ) self:SetNextSecondaryFire( CurTime() + self.Primary.Delay ) end function SWEP:SecondaryAttack() if ( !self:CanSecondaryAttack() ) then return end self:EmitSound( "weapons/nyan/nya" .. math.random( 1, 2 ) .. ".mp3", 100, math.random( 85, 100 ) ) local bullet = {} bullet.Num = 6 bullet.Src = self.Owner:GetShootPos() bullet.Dir = self.Owner:GetAimVector() bullet.Spread = Vector( 0.10, 0.1, 0 ) bullet.Tracer = 1 bullet.Force = 10 bullet.Damage = 9 //bullet.AmmoType = "Ar2AltFire" bullet.TracerName = "rb655_nyan_tracer" self.Owner:FireBullets( bullet ) self.Weapon:SendWeaponAnim( ACT_VM_SECONDARYATTACK ) self.Owner:SetAnimation( PLAYER_ATTACK1 ) self.Weapon:SetNextPrimaryFire( CurTime() + self.Secondary.Delay ) self.Weapon:SetNextSecondaryFire( CurTime() + self.Secondary.Delay ) end function SWEP:Reload() if ( !self.Owner:KeyPressed( IN_RELOAD ) ) then return end if ( self:GetNextPrimaryFire() > CurTime() ) then return end if ( SERVER ) then local ang = self.Owner:EyeAngles() local ent = ents.Create( "ent_nyan_bomb" ) if ( IsValid( ent ) ) then ent:SetPos( self.Owner:GetShootPos() + ang:Forward() * 28 + ang:Right() * 24 - ang:Up() * 8 ) ent:SetAngles( ang ) ent:SetOwner( self.Owner ) ent:Spawn() ent:Activate() local phys = ent:GetPhysicsObject() if ( IsValid( phys ) ) then phys:Wake() phys:AddVelocity( ent:GetForward() * 1337 ) end end end self:SendWeaponAnim( ACT_VM_SECONDARYATTACK ) self.Owner:SetAnimation( PLAYER_ATTACK1 ) self:EmitSound( "weapons/nyan/nya" .. math.random( 1, 2 ) .. ".mp3", 100, math.random( 60, 80 ) ) self:SetNextPrimaryFire( CurTime() + 1 ) self:SetNextSecondaryFire( CurTime() + 1 ) end function SWEP:DoImpactEffect( trace, damageType ) local effectdata = EffectData() effectdata:SetStart( trace.HitPos ) effectdata:SetOrigin( trace.HitNormal + Vector( math.Rand( -0.5, 0.5 ), math.Rand( -0.5, 0.5 ), math.Rand( -0.5, 0.5 ) ) ) util.Effect( "rb655_nyan_bounce", effectdata ) return true end function SWEP:FireAnimationEvent( pos, ang, event ) return true end function SWEP:KillSounds() if ( self.BeatSound ) then self.BeatSound:Stop() self.BeatSound = nil end if ( self.LoopSound ) then self.LoopSound:Stop() self.LoopSound = nil end end function SWEP:OnRemove() self:KillSounds() end function SWEP:OnDrop() self:KillSounds() end function SWEP:Deploy() self.Weapon:SendWeaponAnim( ACT_VM_DRAW ) self.Weapon:SetNextPrimaryFire( CurTime() + self.Weapon:SequenceDuration() ) if ( CLIENT ) then return true end self.BeatSound = CreateSound( self.Owner, Sound( "weapons/nyan/nyan_beat.mp3" ) ) if ( self.BeatSound ) then self.BeatSound:Play() end return true end function SWEP:Holster() self:KillSounds() return true end function SWEP:Think() if ( self.Owner:IsPlayer() && self.Owner:KeyReleased( IN_ATTACK ) ) then if ( self.LoopSound ) then self.LoopSound:ChangeVolume( 0, 0.1 ) end if ( self.BeatSound ) then self.BeatSound:ChangeVolume( 1, 0.1 ) end end end [/CODE]
Thanks, but i don't know how to loop it or what code from the nyan gun to put into my script..... i just started learning lua today lol. I guess i should've mentioned that in the first post. 0_o
[QUOTE=quigon;43457579]Thanks, but i don't know how to loop it or what code from the nyan gun to put into my script..... i just started learning lua today lol. I guess i should've mentioned that in the first post. 0_o[/QUOTE] How do you learn without trying? Read the SWEP:PrimaryAttack() function in the post above and basically copy it and put in your file name for the loop.
Sorry, you need to Log In to post a reply to this thread.