sound.PlayURL issue?

Hello, I have an issue where I get this error whenever I want a song to play on the start of a round in TTT, I put the code clientside. Any ideas?
Error:


[ERROR] addons/new bor music/lua/autorun/client/bormusic.lua:24: attempt to index local 'sound' (a nil value)
  1. unknown - addons/new bor music/lua/autorun/client/bormusic.lua:24

Code:
[lua]local bormusic = {
-snip-
}

hook.Add(“TTTBeginRound”, “MyStartMusic”, function()
if ( GetConVarNumber( “ttt_music_begin_enable” ) == 1 ) then
sound.PlayURL( bormusic[math.random( 1, #bormusic )], “”, function(sound)
sound:Play()
sound:SetVolume( GetConVarNumber( “wyozimc_volume”) )
end )
else return end
end )

if SERVER then return end
[/lua]

Thanks :slight_smile:

[editline]4th May 2014[/editline]

This also happens for only a few players, which I find odd.

sound is listed as shared; but it shouldn’t have an issue.

might not be the best, instead of using math.random, use table.Random( bormusic instead. Any additional info? Are players running clientside scripts that overwrite the sound library reference?

Are you getting the error serverside? You need to move ‘if SERVER then return end’ to the top or it has no effect.

Only clientside.

[editline]4th May 2014[/editline]

And sound.PlayURL is listed as client in the wiki.
It only happens to some select random players though which is what I find weird.

The sound channel is probably invalid.
Wrap the whole thing in an IsValid( snd )

Also if it’s invalid, Maybe make it echo the url to admins or something.

Why did you name your variable “sound”, I was thinking that it can’t find the sound library.

Lol, I’ll change it to music.

I’ll try wrapping it in an IsValid( snd ) but making that echo wouldn’t be too useful because it only happens to some players.

since it’s a random sound from the table, it might be a particular file acting up, which it will pinpoint

Everyone hears the same song, if it errors for some and not the others then how is it the song.

That would be the case if the code was serverside, but since it is clientside, it will select a random song for each client separately.

Next time the error happens ask the player to give you the bass error code that pops up on the console.
It’s usually a red print, the number can be compared to the ones in this list.
It could just be a simple timeout error.

So if I made that serverside it would make it play the same song for everyone?

And thanks Jvs, I’ll look into that :slight_smile:

You can’t really make it serverside unless you send the song name to play in a net message, because sound.PlayURL is clientside only.

So I tried changing some of the stuff you guys said but now the music doesn’t play and there are no errors (clientside or serverside). Here is my current code:

[lua]
if SERVER then return end

if IsValid( snd ) then

local bormusic = {
-snip-
}

hook.Add(“TTTBeginRound”, “MyStartMusic”, function()
if ( GetConVarNumber( “ttt_music_begin_enable” ) == 1 ) then
sound.PlayURL( table.Random(bormusic), “”, function(music)
music:Play()
music:SetVolume( GetConVarNumber( “wyozimc_volume”) )
end )
else return end
end )

else return end
[/lua]

Are you joking?

[lua]
if SERVER then return end

local bormusic = {
-snip-
}

hook.Add(“TTTBeginRound”, “MyStartMusic”, function()
if ( GetConVarNumber( “ttt_music_begin_enable” ) == 1 ) then
sound.PlayURL( table.Random(bormusic), “putSomethingUniqueHere”, function(music)
if ( !IsValid( music ) ) then print(“FUCK ME I FAILED”) return end
music:Play()
music:SetVolume( GetConVarNumber( “wyozimc_volume”) )
end )
end
end )

[/lua]

:pwn: This is why I don’t code.
Thanks though!

Just an FYI, lua doesn’t need ‘else return end’ - you can just do ‘end’

This is true only in case where the “else return end” is the last line of a function.