[code]
weapons/knife/shared.lua:60: attempt to index global 'catbomb' (a nil value)
[/code]
srly, ppl start giving me real worknig code soon?
[QUOTE=Xelander;14660287][code]
weapons/knife/shared.lua:60: attempt to index global 'catbomb' (a nil value)
[/code]
srly, ppl start giving me real worknig code soon?[/QUOTE]
You're the one obviously doing something wrong because that code by it's self should work fine so long as self.Owner is defined.
[QUOTE=Overv;14660180]Have fun rewriting stuff that's been written already.[/QUOTE]
I think his own code is faster in this case, since table.Random actually loops through the table instead of just indexing it.
If table.Random is the better solution for getting a random value from a table, you need to rethink your design because there are better ways.
EG, have an associative table that uses numeric keys, where the values are the keys to your main table and pick a random entry from that to index your intended table.
[QUOTE=Xelander;14660287][code]
weapons/knife/shared.lua:60: attempt to index global 'catbomb' (a nil value)
[/code]
srly, ppl start giving me real worknig code soon?[/QUOTE]
We're trying to help you learn, not do everything for you. Be more helpful instead of demanding an answer. For starters, give us the erroring line.
Jeez, is it really that hard?
[lua]
function SWEP:Initialize()
self.Blah = {
Sound("funniez/catbomb.mp3"),
Sound("funniez/ravebreak.mp3"),
Sound("funniez/speach/obeyyourthirst2.wav"),
Sound("funniez/speach/obeyyourthirstsync.wav"),
Sound("funniez/cant.wav"),
Sound("funniez/orly.mp3"),
Sound("funniez/yarly.mp3") }
end
-- then somewhere in SWEP:PrimaryAttack()
self.Weapon:EmitSound(self.Blah[math.random(#self.Blah)])
end
[/lua]
If you're using this in a SWEP ofcourse.
i think my code dosn't work cuz i think i forgot to save it and let the window stay open, retesting
Edit:
tested it, but this error comes every time i try to start it:
[code]
includes/extensions/table.lua:165: bad argument #1 to 'pairs' (table expected, got nil)
[/code]
...?
Somewhere in your code you are calling "table.Count" or any other function that starts with "table." and passing a value that doesn't exist. Make sure you don't have any spelling errors in your variable names.
only place where the word table is here, i comment lined where it is:
[lua]
function SWEP:SecondaryAttack()
local randomSound = table.Random(randomsounds) //here, but i cant see any error
local randomsounds = {
"funniez/catbomb.mp3",
"funniez/ravebreak.mp3",
"funniez/speach/obeyyourthirst2.wav",
"funniez/speach/obeyyourthirstsync.wav",
"funniez/cant.wav",
"funniez/orly.mp3",
"funniez/yarly.mp3"
}
local random = math.random(1, #randomsounds)
self.Owner:EmitSound(randomsounds[random])
end
[/lua]
idea?
You have to define the table before you use table.Random.
You are using the variable "randomsounds" before you are defining it. Just remove that line.
[B]Edit:[/B]
I was not fast enough.
exactly what line? cuz im an idiot?
You are using randomSounds before you define it. Swap these lines:
[lua]local randomSound = table.Random(randomsounds)[/lua]
[lua]local randomsounds = {
"funniez/catbomb.mp3",
"funniez/ravebreak.mp3",
"funniez/speach/obeyyourthirst2.wav",
"funniez/speach/obeyyourthirstsync.wav",
"funniez/cant.wav",
"funniez/orly.mp3",
"funniez/yarly.mp3"
}[/lua]
ok done that testin now
Edit: Thanks! work perfectly fine now!
[b]zomg there's a second page[/b]
He obviously has no idea what the fuck he is doing.
[lua]
SWEP.Sounds = {
Sound("funniez/catbomb.mp3" ),
Sound("funniez/ravebreak.mp3" ),
Sound("funniez/speach/obeyyourthirst2.wav" ),
Sound("funniez/speach/obeyyourthirstsync.wav" ),
Sound("funniez/cant.wav" ),
Sound("funniez/orly.mp3" ),
Sound("funniez/yarly.mp3" ),,
}
function SWEP:SecondaryAttack()
self.Owner:EmitSound(table.Random(self.Sounds))
end
[/lua]
[url=http://wiki.garrysmod.com/wiki/?title=Lua]Go learn Lua[/url].
been there alot, cant find anything about random there...
[QUOTE=Xelander;14670096]been there alot, cant find anything about random there...[/QUOTE]
Learn to use [url=http://www.lmgtfy.com/?q=site:http://wiki.garrysmod.com/wiki/ random]Google[/url], or the search feature on the left hand side of the any page in the Wiki.
oh lol :/ ok i will try
Dono what you did wrong with my code.. but it's exactly the same as i used in my Sweps for ActioN ( except the sounds a different ).
This is so entertaining: I wanna play! :V:
[lua]function SWEP:SecondaryAttack()
local randomsounds = {
"funniez/catbomb.mp3",
"funniez/ravebreak.mp3",
"funniez/speach/obeyyourthirst2.wav",
"funniez/speach/obeyyourthirstsync.wav",
"funniez/cant.wav",
"funniez/orly.mp3",
"funniez/yarly.mp3"
}
//You don't need this, where is it even being called from?
//But if you DID, you would have it AFTER declaring the table
//local randomSound = table.Random( randomsounds )
local random = math.random( 1, #randomsounds )
self.Owner:EmitSound( Sound( randomsounds[random] ) )
end
[/lua]
[QUOTE=Zcom;14670997]This is so entertaining: I wanna play! :V:
[lua]function SWEP:SecondaryAttack()
local randomsounds = {
"funniez/catbomb.mp3",
"funniez/ravebreak.mp3",
"funniez/speach/obeyyourthirst2.wav",
"funniez/speach/obeyyourthirstsync.wav",
"funniez/cant.wav",
"funniez/orly.mp3",
"funniez/yarly.mp3"
}
//You don't need this, where is it even being called from?
//But if you DID, you would have it AFTER declaring the table
//local randomSound = table.Random( randomsounds )
local random = math.random( 1, #randomsounds )
self.Owner:EmitSound( Sound( randomsounds[random] ) )
end
[/lua][/QUOTE]
[b]Why the hell are you defining the table in the attack function?[/b]
Yay for epic fucking garbage spam.
[QUOTE=Deco Da Man;14671060][b]Why the hell are you defining the table in the attack function?[/b]
Yay for epic fucking garbage spam.[/QUOTE]
Man, I thought [b]I[/b] was pissy today. You should take your meds and go to sleep dude, seriously. :P
[QUOTE=Deco Da Man;14669998][b]zomg there's a second page[/b]
He obviously has no idea what the fuck he is doing.
[lua]
function SWEP:SecondaryAttack()
self.Owner:EmitSound(table.Random(self.Sounds))
end
[/lua]
[/QUOTE]
I'd use self.Weapon:EmitSound instead of self.Owner:EmitSound because of my expirience with self.Owner in multiplayer it emits 2-3 times the same sound.
[QUOTE=L337N008;14671578]I'd use self.Weapon:EmitSound instead of self.Owner:EmitSound because of my expirience with self.Owner in multiplayer it emits 2-3 times the same sound.[/QUOTE]
You need ensure that it only runs on the server.
A simple "if CLIENT then return end" will suffice.
[QUOTE=Zcom;14671282]Man, I thought [b]I[/b] was pissy today. You should take your meds and go to sleep dude, seriously. :P[/QUOTE]
He made a valid statement. Define your large tables outside of frequently called functions.
-snip-
[QUOTE=L337N008;14671578]I'd use self.Weapon:EmitSound instead of self.Owner:EmitSound because of my expirience with self.Owner in multiplayer it emits 2-3 times the same sound.[/QUOTE]
OPS. silly mistake
[QUOTE=Deco Da Man;14669998][b]zomg there's a second page[/b]
He obviously has no idea what the fuck he is doing.
[lua]
SWEP.Sounds = {
Sound("funniez/catbomb.mp3" ),
Sound("funniez/ravebreak.mp3" ),
Sound("funniez/speach/obeyyourthirst2.wav" ),
Sound("funniez/speach/obeyyourthirstsync.wav" ),
Sound("funniez/cant.wav" ),
Sound("funniez/orly.mp3" ),
Sound("funniez/yarly.mp3" ),,
}
function SWEP:SecondaryAttack()
self.Owner:EmitSound(table.Random(self.Sounds))
end
[/lua]
[url=http://wiki.garrysmod.com/wiki/?title=Lua]Go learn Lua[/url].[/QUOTE]
but what are you supposed to replace
function SWEP:SecondaryAttack()
self.Owner:EmitSound(table.Random(self.[B]Sounds[/B]))
with?
Sorry, you need to Log In to post a reply to this thread.