• My SWEP don't spawn (Noobie question)
    5 replies, posted
Hi everybody (again), i'm making a Whistle that if you hold left on the mouse will sound. My SWEP appears on the Weapons menu, but when i click, didn't show up, and there isn't any error on the console: Here is my code: SWEP.PrintName = "Police Whistle" SWEP.Author = "ItzDannio25" SWEP.Contact = "inklingsplat25@gmail.com" SWEP.Purpose = "Whistle for cops!" SWEP.Instructions = "Left click to sound" SWEP.Category = "Police" SWEP.Spawnable= true SWEP.AdminSpawnable= true SWEP.AdminOnly = false SWEP.ViewModelFOV = 70 SWEP.ViewModel = "models/weapons/v_smg1.mdl" SWEP.WorldModel = "models/weapons/w_smg1.mdl" SWEP.ViewModelFlip = false SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false SWEP.UseHands = true SWEP.HoldType = "Pistol"  SWEP.DrawAmmo = false SWEP.DrawCrosshair = false if ( SERVER ) then AddCSLuaFile(weapon_whistle.lua) resource.AddFile( "sound/weapons/whistle/whistlesound.mp3" ) resource.AddFile( "model/weapons/sv_smg1.mdl" ) resource.AddFile( "materials/whistle/selectionwhistle.png" ) resource.AddFile( "materials/whistle/whistle.vtf" ) end if ( CLIENT ) then SWEP.SlotPos = 3 SWEP.Slot = 1 SWEP.WepSelectIcon = Material( "nyan/selection.png" ) SWEP.BounceWeaponIcon = false     SWEP.DrawWeaponInfoBox = false end 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" 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( "sound/whistle/whistlesound.mp3" .. math.random( 1, 2 ) .. ".mp3", 100, math.random( 60, 80 ) ) elseif ( self.LoopSound ) then self.LoopSound:ChangeVolume( 1, 0.1 ) else self.LoopSound = CreateSound( self.Owner, Sound( "sound/whistle/whistlesound.mp3" ) ) if ( self.LoopSound ) then self.LoopSound:Play() end end if ( self.BeatSound ) then self.BeatSound:ChangeVolume( 0, 0.1 ) end SWEP.EquipMenuData = {       name = "Whistle",       type = "item_weapon",       desc = "Play a whistle sound"    }; SWEP.Kind = WEAPON_EQUIP SWEP.CanBuy = {ROLE_DETECTIVE, ROLE_TRAITOR} SWEP.LimitedStock = false SWEP.Icon = "whistle/selectionwhistle.png" end (swep model is temporaly) Any one know what happen? I'm new and i started Lua today.
Couple of things: Things like SWEP.SlotPos shouldnt be inside the if CLIENT statement Putting the if CLIENT statement inside the primaryattack function is a better idea in case you need to do server stuff in there too Is more of a syntax thing, Try to space things out and not to write all your stuff on 1 line, use tabulations, etc, put things variables like SWEP.Kind at the top and functions at the bottom. Here's an example of how to space things out to make it more readable: SWEP.PrintName = "Police Whistle" SWEP.Author = "ItzDannio25" SWEP.Contact = "inklingsplat25@gmail.com" SWEP.Purpose = "Whistle for cops!" SWEP.Instructions = "Left click to sound" if ( SERVER ) then AddCSLuaFile(weapon_whistle.lua) resource.AddFile( "sound/weapons/whistle/whistlesound.mp3" ) resource.AddFile( "model/weapons/sv_smg1.mdl" ) resource.AddFile( "materials/whistle/selectionwhistle.png" ) resource.AddFile( "materials/whistle/whistle.vtf" ) end  SWEP.Category = "Police" SWEP.Spawnable= true SWEP.AdminSpawnable= true SWEP.AdminOnly = false SWEP.ViewModelFOV = 70 SWEP.ViewModel = "models/weapons/v_smg1.mdl" SWEP.WorldModel = "models/weapons/w_smg1.mdl" SWEP.ViewModelFlip = false SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false SWEP.UseHands = true SWEP.HoldType = "Pistol"  SWEP.DrawAmmo = false SWEP.DrawCrosshair = false SWEP.SlotPos = 3 SWEP.Slot = 1 SWEP.WepSelectIcon = Material( "nyan/selection.png" ) SWEP.BounceWeaponIcon = false SWEP.DrawWeaponInfoBox = 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.EquipMenuData = {       name = "Whistle",       type = "item_weapon",       desc = "Play a whistle sound"    }; SWEP.Kind = WEAPON_EQUIP SWEP.CanBuy = {ROLE_DETECTIVE, ROLE_TRAITOR} SWEP.LimitedStock = false SWEP.Icon = "whistle/selectionwhistle.png" function SWEP:PrimaryAttack() if CLIENT then if ( !self:CanPrimaryAttack() ) then return end  if (self.Owner:IsNPC()) then self:EmitSound( "sound/whistle/whistlesound.mp3" .. math.random( 1, 2 ) .. ".mp3", 100, math.random( 60, 80 ) ) elseif ( self.LoopSound ) then self.LoopSound:ChangeVolume( 1, 0.1 ) else self.LoopSound = CreateSound( self.Owner, Sound( "sound/whistle/whistlesound.mp3" ) ) end if ( self.LoopSound ) then  self.LoopSound:Play()  end  if ( self.BeatSound ) then  self.BeatSound:ChangeVolume( 0, 0.1 ) end end end if CLIENT then 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  end
Thanks, it works but now when i hold left on the mouse, there is no sound, only the sound of recharging. I modifiqued the PrimaryAttack part: SWEP:PrimaryAttack()if ( !self:CanPrimaryAttack() ) then     self.EmitSound ("sound/whistle/whistlesound.mp3", 150, 100, 1, CHAN_AUTO) elseif ( self.LoopSound ) then self.LoopSound:ChangeVolume( 1, 0.1 ) else self.LoopSound = CreateSound( self.Owner, Sound( "sound/whistle/whistlesound.mp3" ) ) if ( self.LoopSound ) then self.LoopSound:Play() end  Now the recharging sound isn't there, but same, tere is no sound playing, any solution?
Found a way to do it here self.Start = 0 function SWEP:Initialize() self.LoopSound = CreateSound( self.Owner, Sound("sound/whistle/whistlesound.mp3") ) end function SWEP:PrimaryAttack() if ( self.Start == 0 ) then AnnoySound:Play() self.Start = 1 end timer.Create("uber_tmr",0.5,1,function() --Change the timer duration here AnnoySound:Stop() self.Start = 0 end) self.Weapon:SetNextPrimaryFire( CurTime() + 0.02 ) end
Thanks, i fixed some errors that gmod showed me, but there is one error that i can't fix it, that error is: [ERROR] lua/weapons/weapon_whistle.lua:69: attempt to index global 'self' (a nil value)   1. unknown - lua/weapons/weapon_whistle.lua:69 and his line of code is this: self.Start = falsefunction SWEP:Initialize() (it's from your code) any solution?
Yeah that's my bad, since the self.Start is inside the timer function it doesn't recognize self as the weapon anymore. Replace self.Start = false with local Start = false Then replace the self.Start inside the function as just Start = false and Start = true
Sorry, you need to Log In to post a reply to this thread.