• A swep problem
    14 replies, posted
I am creating a sound swep for a server on Garrys Mod and am coming across some issues when it comes to the swep itself. Upon firing, the intended action is to play one of 21 lines I have loaded into the files. Instead, it doesnt play any sound what so ever. Ive redone the code over and over and come out with the same results: A silent swep. Here is the LUA coding for the gun: if SERVER then AddCSLuaFile() end if CLIENT then SWEP.PrintName = "Hidden Sounds" SWEP.Author = "Dead Eye" SWEP.Slot = 0 SWEP.SlotPos = 1 end SWEP.Category = "Other" SWEP.ViewModelFlip = false SWEP.ViewModelFOV = 60 SWEP.Spawnable = true SWEP.AdminOnly = false SWEP.UseHands = false SWEP.ViewModel = "models/weapons/c_arms_hev.mdl" SWEP.WorldModel = "" SWEP.Weight = 1 SWEP.AutoSwitchTo = true SWEP.AutoSwitchFrom = true SWEP.Primary.Recoil = 0 SWEP.Primary.ClipSize = -1 SWEP.Primary.DefaultClip = -1 SWEP.Primary.Automatic = false SWEP.Primary.Ammo = "none" SWEP.Secondary.ClipSize = -1 SWEP.Secondary.DefaultClip = -1 SWEP.Secondary.Automatic = false SWEP.Secondary.Ammo = "none" function SWEP:Initialize() timer.Simple(0.2, function() self:SetHoldType("idle") end) self:SetHoldType("idle") end function SWEP:PrimaryAttack() local randomsounds = { "weapons/voice/617-behindyou", "weapons/voice/617-behindyou01", "weapons/voice/617-behindyou02", "weapons/voice/617-imhere", "weapons/voice/617-imhere01", "weapons/voice/617-imhere02", "weapons/voice/617-imhere03", "weapons/voice/617-imhere04", "weapons/voice/617-iseeyou", "weapons/voice/617-iseeyou01", "weapons/voice/617-iseeyou02", "weapons/voice/617-iseeyou03", "weapons/voice/617-lookup", "weapons/voice/617-lookup01", "weapons/voice/617-lookup02", "weapons/voice/617-lookup03", "weapons/voice/617-overhere01", "weapons/voice/617-overhere02", "weapons/voice/617-overhere03", "weapons/voice/617-turnaround01", "weapons/voice/617-turnaround02" } local random = math.random(1, #randomsounds) self.Weapon:EmitSound(table.Random(randomsounds)) end function SWEP:SecondaryAttack() end function SWEP:Reload() end Is there a visible issue with the coding? I'd love some feedback on what I may be doing wrong.
Emit the sound from the owner, not the weapon. Also, declare the sound table outside of the table and use math.random instead of table.Random (just like you did with the random variable, but use it to access the index in the table).
[QUOTE=code_gs;51792147]Emit the sound from the owner, not the weapon. Also, declare the sound table outside of the table and use math.random instead of table.Random (just like you did with the random variable, but use it to access the index in the table).[/QUOTE] local random = math.random(1, #randomsounds) self.Owner:EmitSound(math.Random(randomsounds)) Like this? EDIT: Still doesnt work. Still silence
It would be self.Owner:EmitSound (sounds [math.random(1, #randomsounds)])
[QUOTE=code_gs;51792189]It would be self.Owner:EmitSound (sounds [math.random(1, #randomsounds)])[/QUOTE] I did that and ended up with this console error: [ERROR] addons/the hidden sounds swep/lua/weapons/swep_hiddenvoice.lua:74: attempt to index global 'sounds' (a nil value) 1. unknown - addons/the hidden sounds swep/lua/weapons/swep_hiddenvoice.lua:74
sounds was just a placeholder. Use your table name instead.
[QUOTE=code_gs;51792270]sounds was just a placeholder. Use your table name instead.[/QUOTE] The table name is... local random sounds and I place that in the position of SOunds on the emit line?
No, you are creating the table, then accessing it. For example, [code]local mytable = { "foo" } function MyFunc () print (mytable [1]) end [/code] That will create a table, then access it from inside of the function.
[QUOTE=code_gs;51792348]No, you are creating the table, then accessing it. For example, [code]local mytable = { "foo" } function MyFunc () print (mytable [1]) end [/code] That will create a table, then access it from inside of the function.[/QUOTE] Im very unfamiliar with LUA and actually used a sound swep as a template for this swep. So the local mytable would go above the primary fire function or in it? Then I would access the table by putting what?
You would declare the table outside of the function, then access it inside by doing tablename [element]. In this case, the element is the math.random call.
[QUOTE=code_gs;51792383]You would declare the table outside of the function, then access it inside by doing tablename [element]. In this case, the element is the math.random call.[/QUOTE] local randomsounds = { "weapons/voice/617-behindyou", "weapons/voice/617-behindyou01", "weapons/voice/617-behindyou02", "weapons/voice/617-imhere", "weapons/voice/617-imhere01", "weapons/voice/617-imhere02", "weapons/voice/617-imhere03", "weapons/voice/617-imhere04", "weapons/voice/617-iseeyou", "weapons/voice/617-iseeyou01", "weapons/voice/617-iseeyou02", "weapons/voice/617-iseeyou03", "weapons/voice/617-lookup", "weapons/voice/617-lookup01", "weapons/voice/617-lookup02", "weapons/voice/617-lookup03", "weapons/voice/617-overhere01", "weapons/voice/617-overhere02", "weapons/voice/617-overhere03", "weapons/voice/617-turnaround01", "weapons/voice/617-turnaround02" } function SWEP:PrimaryAttack() local random = math.random(1, #randomsounds) self.Owner:EmitSound (sounds [math.random(1, #randomsounds)]) Like this? I think I got it down. I just need what I would put into the place of the primary attack so it would make the sound.
You are still accessing the table "sounds" even though the name of your table is "randomsounds." Also, there's no need to declare the variable "random" since its never used.
[QUOTE=code_gs;51792478]You are still accessing the table "sounds" even though the name of your table is "randomsounds." Also, there's no need to declare the variable "random" since its never used.[/QUOTE] Got the table fixed, but dont know which "random" you are refering to.
The local variable "random". It's useless since you are never using it.
[QUOTE=code_gs;51792903]The local variable "random". It's useless since you are never using it.[/QUOTE] local random = math.random(1, #randomsounds) self.Owner:EmitSound (randomsounds [math.random(1, #randomsounds)]) So delete the random on the first line? I tried that and it wont even load the swep in, saying it needs a name near the =. EDIT: I fixed it! I just needed to place .mp3 at the end of every line. Thank you very much for your help and time!
Sorry, you need to Log In to post a reply to this thread.