Random Song Picker

I’ve tried to get help for this before but the script didn’t work. I’m trying to make a script that chooses and plays a random song (out of 3) when anyone wins (whether traitor innocent or clock). Here is what I have so far.

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)

It looks correct to me but it wouldn’t work, I would really appreciate the help, thanks!

WIN_INNOCENT, WIN_TRAITOR, and WIN_TIMELIMIT are nil.

What do you mean?

[editline]25th November 2012[/editline]

Like how would I fix it?

He means that you need to have defined them somewhere else in the script as global variables, which I just assumed you had done.

This is to say you need to have this somewhere :

[lua]WIN_INNOCENT = 1
WIN_TRAITOR = 2
WIN_TIMELIMIT = 3[/lua]

If you really want to pass the words instead of a number disguised as words you need to use strings. (“WIN_INNOCENT”)

Oh thank you!

[editline]26th November 2012[/editline]

So would this be correct then?

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

WIN_INNOCENT = 1
WIN_TRAITOR = 2
WIN_TIMELIMIT = 3

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)