Need some help with coding/language/syntax

I just started scripting in LUA and haven’t quite figuered out how to make nested if statement (i think its the problem).
Here is my code:
resource.AddFile(“sound/rg/SmKsong.mp3”)
resource.AddFile(“sound/rg/louderandprouder.mp3”)
resource.AddFile(“sound/rg/superfun.mp3”)

local function PlayMusic(wintype)

if wintype == WIN_INNOCENT then
x = math.random(3)
if x = 1 then
BroadcastLua(‘surface.PlaySound(“rg/SmKsong.mp3”)’)
elseif x = 2 then
BroadcastLua('surface.PlaySound(“rg/superfun.mp3”) ')
elseif x = 3 then
BroadcastLua(‘surface.PlaySound(“rg/louderandprouder.mp3”)’)
end
end

if wintype == WIN_TRAITOR then
y = math.random(3)
if y = 1 then
BroadcastLua(‘surface.PlaySound(“rg/SmKsong.mp3”)’)
elseif y = 2 then
BroadcastLua('surface.PlaySound(“rg/superfun.mp3”) ')
elseif y = 3 then
BroadcastLua(‘surface.PlaySound(“rg/louderandprouder.mp3”)’)
end
end

if wintype == WIN_TIMELIMIT then
z = math.random(3)
if z = 1 then
BroadcastLua(‘surface.PlaySound(“rg/SmKsong.mp3”)’)
elseif z = 2 then
BroadcastLua('surface.PlaySound(“rg/superfun.mp3”) ')
elseif z = 3 then
BroadcastLua(‘surface.PlaySound(“rg/louderandprouder.mp3”)’)
end
end
hook.Add(“TTTEndRound”, “MyMusic”, PlayMusic)

I need it to play a random song out of those three no matter who wins.
Can you help me out and tell me what i did wrong?

This isn’t exactly what you asked for but this is how I would structure it.

[LUA]resource.AddFile(“sound/rg/SmKsong.mp3”)
resource.AddFile(“sound/rg/louderandprouder.mp3”)
resource.AddFile(“sound/rg/superfun.mp3”)

local SoundCollections = {}

SoundCollections[WIN_INNOCENT] = {‘surface.PlaySound(“rg/SmKsong.mp3”)’,
'surface.PlaySound(“rg/superfun.mp3”) ',
‘surface.PlaySound(“rg/louderandprouder.mp3”)’}

SoundCollections[WIN_TRAITOR] = {‘surface.PlaySound(“rg/SmKsong.mp3”)’,
'surface.PlaySound(“rg/superfun.mp3”) ',
‘surface.PlaySound(“rg/louderandprouder.mp3”)’}

SoundCollections[WIN_TIMELIMIT] = {‘surface.PlaySound(“rg/SmKsong.mp3”)’,
'surface.PlaySound(“rg/superfun.mp3”) ',
‘surface.PlaySound(“rg/louderandprouder.mp3”)’}

local function PlayMusic(wintype)
BroadcastLua(SoundCollections[wintype][math.random(#SoundCollections)])
end
hook.Add(“TTTEndRound”, “MyMusic”, PlayMusic)[/LUA]

Also your error is when you check the value of x (x should be local by the way).

= is an assignment operator.

== is a comparison operator.

So you should’ve been using x == 1 instead of x =1. This code definitely should have given you error messages.

This being said I also wouldn’t use BroadcastLua. TTTEndRound is a shared hook so you can execute your code clientside :

[LUA]-- This part on the server
resource.AddFile(“sound/rg/SmKsong.mp3”)
resource.AddFile(“sound/rg/louderandprouder.mp3”)
resource.AddFile(“sound/rg/superfun.mp3”)

– This part on the client
local SoundCollections = {}
SoundCollections[WIN_INNOCENT] = {“rg/SmKsong.mp3”,
“rg/superfun.mp3”,
“rg/louderandprouder.mp3”}

SoundCollections[WIN_TRAITOR] = {“rg/SmKsong.mp3”,
“rg/superfun.mp3”,
“rg/louderandprouder.mp3”}

SoundCollections[WIN_TIMELIMIT] = {“rg/SmKsong.mp3”,
“rg/superfun.mp3”,
“rg/louderandprouder.mp3”}

local function PlayMusic(wintype)
surface.PlaySound(SoundCollections[wintype][math.random(#SoundCollections)])
end
hook.Add(“TTTEndRound”, “MyMusic”, PlayMusic)[/LUA]

Of course if you don’t actually have more than 3 musics you don’t need to have it be as complex.

By the way use the [ lua][ /lua] tags when posting code.

TYVM! =3

[editline]24th November 2012[/editline]

Wait would soundcollections be empty or would it have all the music in it?

SoundCollections contains the 3 other tables that each contain the music. See how I assign values to it in the following lines.

Oh thanks!:dance: