• Why can't I set a variable on a swep?
    4 replies, posted
Hello, I don't usually do sweps, however, I'm setting a variable on SWEP:Initialize(). But when I try accessing it on SWEP:Deploy(), I get an error saying the variable I set in Initialize is nil! [lua] function SWEP:Initialize() self.MLG = CreateSound( self, "weapons/interv/mlg.wav" ); end function SWEP:Deploy() self.MLG:Play() return true end [/lua] [lua] [ERROR] shared.lua:139: attempt to index field 'MLG' (a nil value) 1. unknown - shared.lua:139 [/lua]
[code] function SWEP:Deploy() self:EmitSound("weapons/interv/mlg.wav"); return true end [/code]
[QUOTE=sackcreator54;45615148]Hello, I don't usually do sweps, however, I'm setting a variable on SWEP:Initialize(). But when I try accessing it on SWEP:Deploy(), I get an error saying the variable I set in Initialize is nil! [lua] function SWEP:Initialize() self.MLG = CreateSound( self, "weapons/interv/mlg.wav" ); end function SWEP:Deploy() self.MLG:Play() return true end [/lua] [lua] [ERROR] shared.lua:139: attempt to index field 'MLG' (a nil value) 1. unknown - shared.lua:139 [/lua][/QUOTE] Make sure the variable is being declared and used in the same scope. use "if SERVER then" and "if CLIENT then" to separate the scopes.
[QUOTE=sackcreator54;45615148]Hello, I don't usually do sweps, however, I'm setting a variable on SWEP:Initialize(). But when I try accessing it on SWEP:Deploy(), I get an error saying the variable I set in Initialize is nil! [lua] function SWEP:Initialize() self.MLG = CreateSound( self, "weapons/interv/mlg.wav" ); end function SWEP:Deploy() self.MLG:Play() return true end [/lua] [lua] [ERROR] shared.lua:139: attempt to index field 'MLG' (a nil value) 1. unknown - shared.lua:139 [/lua][/QUOTE] Sounds for weapons are done a little bit differently: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/sound/adding_various_types_of_sounds.lua.html[/url] Don't use CreateSound, use sound.Add to set it up. If you try using a straight .wav it'll start acting screwy and won't "report" for each shot all the time depending how many of the same sound is playing. The DropBox shows how to set it up for weapons, and how to then use them.
[QUOTE=Acecool;45618346]Sounds for weapons are done a little bit differently: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/sound/adding_various_types_of_sounds.lua.html[/url] Don't use CreateSound, use sound.Add to set it up. If you try using a straight .wav it'll start acting screwy and won't "report" for each shot all the time depending how many of the same sound is playing. The DropBox shows how to set it up for weapons, and how to then use them.[/QUOTE] The thing is, I WOULD use emitsound, however, I need to use CreateSound. I'm gonna create the sound, play it on deploy with 10% volume, then on fire, change the volume to 100%. I need to all do this seamlessly with the audio.
Sorry, you need to Log In to post a reply to this thread.