Trouble in Terrorist Town Help (Sounds)

Hello everyone, I’ve recently made a new TTT server and I would like some help with the coding. I would like to make it play music at the end of a round. So far, here’s a script:

resource.AddFile(“sound/traitor_win.mp3”)
resource.AddFile(“sound/inno_win.mp3”)
resource.AddFile(“sound/time_win.mp3”)

local function PlayMusic(wintype)
if wintype == WIN_INNOCENT then
BroadcastLua(‘surface.PlaySound(“inno_win.mp3”)’)

elseif wintype == WIN_TRAITOR then
BroadcastLua(‘surface.PlaySound(“traitor_win.mp3”)’)

elseif wintype == WIN_TIMELIMIT then
BroadcastLua(‘surface.PlaySound(“time_win.mp3”)’)
end
end
hook.Add(“TTTEndRound”, “MyMusic”, PlayMusic)

I’d like it to play a song at random, NOT DEPENDING ON WHO WINS ; and I’d like it to stop as soon as the next round starts, Thanks!

Thanks.

We also tried this:

resource.AddFile(“sound/ttt/1.mp3”)
resource.AddFile(“sound/ttt/2.wav”)
resource.AddFile(“sound/ttt/3.mp3”)
resource.AddFile(“sound/ttt/4.mp3”)
resource.AddFile(“sound/ttt/5.mp3”)
resource.AddFile(“sound/ttt/6.mp3”)
resource.AddFile(“sound/ttt/7.wav”)
resource.AddFile(“sound/ttt/8.wav”)
resource.AddFile(“sound/ttt/9.mp3”)
resource.AddFile(“sound/ttt/10.mp3”)

local music = {
Sound(“sound/ttt/1.mp3”),
Sound(“sound/ttt/2.wav”),
Sound(“sound/ttt/3.mp3”),
Sound(“sound/ttt/4.mp3”),
Sound(“sound/ttt/5.mp3”),
Sound(“sound/ttt/6.mp3”),
Sound(“sound/ttt/7.wav”),
Sound(“sound/ttt/8.wav”),
Sound(“sound/ttt/9.mp3”),
Sound(“sound/ttt/10.mp3”)
};

local function PlayMusic(wintype)
if wintype == WIN_INNOCENT then
BroadcastLua(‘surface.PlaySound(table.Random(music))’)

elseif wintype == WIN_TRAITOR then
BroadcastLua(‘surface.PlaySound(table.Random(music))’)

elseif wintype == WIN_TIMELIMIT then
BroadcastLua(‘surface.PlaySound(table.Random(music))’)
end
end
hook.Add(“TTTEndRound”, “MyMusic”, PlayMusic)

 I fail with LUA, so I have no idea what I am doing. Could anyone tell me

why wont this work?

IIRC, surface.PlaySound is already in the sound directory so sound/ttt/1.mp3 -> ttt/1.mp3

[editline]28th June 2011[/editline]

Also you need to escape
BroadcastLua(‘surface.PlaySound(table.Random(music ))’)
to
BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])

[lua]
resource.AddFile(“sound/ttt/blow.mp3”)

local music = {
Sound(“ttt/blow.mp3”),

};

local function PlayMusic(wintype)
if wintype == WIN_INNOCENT then
BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])

elseif wintype == WIN_TRAITOR then
BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])

elseif wintype == WIN_TIMELIMIT then
BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])
end
end
hook.Add(“TTTEndRound”, “MyMusic”, PlayMusic)
[/lua]

i modified like this, but it was not works…

Hmm…
It didn’t seem to work. I even tried using songs from counter-strike and hl2. Here
is what I used:

local music = {
Sound(“ambient/music/country_rock_am_radio_loop.wav”),
Sound(“music/ravenholm_1.mp3”),
Sound(“music/hl2_song15.mp3”),
Sound(“ambient/music/cubanmusic1.wav”),
Sound(“ambient/music/flamenco.wav”),
Sound(“ambient/music/latin.wav”),
Sound(“ambient/music/looping_radio_mix.wav”),
Sound(“ambient/music/mirame_radio_thru_wall.wav”),
Sound(“ambient/music/dustmusic2.wav”),
Sound(“ambient/music/dustmusic3.wav”)
};

local function PlayMusic(wintype)
if wintype == WIN_INNOCENT then
BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])

elseif wintype == WIN_TRAITOR then
BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])

elseif wintype == WIN_TIMELIMIT then
BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])
end
end
hook.Add(“TTTEndRound”, “MyMusic”, PlayMusic)

The lua file is currently in lua/autorun.

Bcuz WIN_INNOCENT isn’t a team add it to autorun/server and find the proper TEAM TEAM ya know like

TEAM_TRAITOR or something

The teams are correct, and I’m only playing the sounds once the round is over. I still need help, and I’m still open to suggestions. I can make sounds play, but I’d like random sounds to play instead of the same one over and over. Please HELP!

How did you do that? please help me

i wrote like that

[lua]
resource.AddFile(“sound/ttt/blow.mp3”)

local music = {
Sound(“ttt/blow.mp3”),

};

local function PlayMusic(wintype)
if wintype == WIN_INNOCENT then
BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])

elseif wintype == WIN_traitor then
BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])

elseif wintype == WIN_TIMELIMIT then
BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])
end
end
hook.Add(“TTTEndRound”, “MyMusic”, PlayMusic)
[/lua]

what is the wrong point?

instead of table.Random(music) try music[math.random(1, table.GetLastValue)]

And WIN_INNOCENT isn’t a team. It’s a variable called when the innocent(s) / detective(s) are the last alive or kill off the traitor(s)

Here’s what I did, and It works right now:
[lua]
resource.AddFile(“sound/ttt/traitor_win.mp3”)
resource.AddFile(“sound/ttt/inno_win.mp3”)
resource.AddFile(“sound/ttt/time_win.mp3”)

local function PlayMusic(wintype)
if wintype == WIN_INNOCENT then
BroadcastLua(‘surface.PlaySound(“ttt/inno_win.mp3”)’)

elseif wintype == WIN_TRAITOR then
BroadcastLua('surface.PlaySound(“ttt/traitor_win.mp3”) ')

elseif wintype == WIN_TIMELIMIT then
BroadcastLua(‘surface.PlaySound(“ttt/time_win.mp3”)’)
end
end
hook.Add(“TTTEndRound”, “MyMusic”, PlayMusic)
[/lua]
That’s what I use at the moment, and it works wonderfully right now. I wanted to make it choose a song from random to play from my selected sounds.

InfernalCookie, Could you please input your suggestion into the code? I’m a little off where to put it.

[lua]
local function PlayMusic(wintype)
if wintype == WIN_INNOCENT then

BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])

elseif wintype == WIN_TRAITOR then

BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])

elseif wintype == WIN_TIMELIMIT then
BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])
end
end
[/lua]

instead of table.Random(music) try music[math.Random(1,table.GetLastValue)]

although I don’t think that’s the issue. I think the issue is because it’s inputting Sound(“ambient/music/cubanmusic1.wav”), instead of the actual string, which is what it needs to do. Try making it just the string?

Alright, I’ll mess around with it and see if anything happens :slight_smile:

[editline]28th June 2011[/editline]
I messed around with it, and nothing worked :C Here’s the script:
[lua]
resource.AddFile(“sound/ttt/horse.mp3”)
resource.AddFile(“sound/ttt/trollface.mp3”)
resource.AddFile(“sound/ttt/pwnedftw.wav”)
resource.AddFile(“sound/ttt/thisfire.mp3”)
resource.AddFile(“sound/ttt/snakeeater.mp3”)
resource.AddFile(“sound/ttt/pwnsong.mp3”)

local music = {
Sound(“ttt/pwnedftw.wav”),
Sound(“ttt/snakeeater.mp3”),
Sound(“ttt/thisfire.mp3”),
Sound(“ttt/trollface.mp3”),
Sound(“ttt/pwnsong.mp3”),
Sound(“ttt/horse.mp3”),
};

local function PlayMusic(wintype)
if wintype == WIN_INNOCENT then
BroadcastLua([[surface.PlaySound(]]…music[math.Random(1,table.GetLastValue)]

elseif wintype == WIN_TRAITOR then
BroadcastLua([[surface.PlaySound(]]…music[math.Random(1,table.GetLastValue)]

elseif wintype == WIN_TIMELIMIT then
BroadcastLua([[surface.PlaySound(]]…music[math.Random(1,table.GetLastValue)]
end
end
hook.Add(“TTTEndRound”, “MyMusic”, PlayMusic)
[/lua]
I don’t see what i’m doing wrong, and I’d like professional help. I am not a good LUA scripter, and I suck ass at coding. Anymore suggestions?

Bump

Replace this with your script

[lua]local function PlayMusic(wintype)
print(wintype)
end
hook.Add(“TTTEndRound”, “MyMusic”, PlayMusic)[/lua]

And type kill in console a few times even when traitor detective and innocent and give us the console output

I’ve found your issue now. This is in init.lua, I hope you can figure out what to do with the following code, if not we can help. :slight_smile:

[lua]
function EndRound(type)
ServerLog("Round ended.
")

---------- most important vv

if type == WIN_TIMELIMIT then
LANG.Msg(“win_time”)
ServerLog("Result: timelimit reached, traitors lose.
")
elseif type == WIN_TRAITOR then
LANG.Msg(“win_traitor”)
ServerLog("Result: traitors win.
")
elseif type == WIN_INNOCENT then
LANG.Msg(“win_innocent”)
ServerLog("Result: innocent win.
")
else
ServerLog("Result: unknown victory condition!
")
end

-------- most important ^^

– first handle round end
SetRoundState(ROUND_POST)

local ptime = math.max(5, GetConVar(“ttt_posttime_seconds”):GetInt())
LANG.Msg(“win_showreport”, {num = ptime})
timer.Create(“end2prep”, ptime, 1, PrepareRound)

– Piggyback on “round end” time global var to show end of phase timer
SetRoundEnd(CurTime() + ptime)

timer.Create(“restartmute”, ptime - 1, 1, MuteForRestart, true)

– Stop checking for wins
StopWinChecks()
– … and it keeps going but the above is the most important.
[/lua]

[editline]29th June 2011[/editline]

Just noticed something, the function at 755 in init.lua
[lua]
function GM:MapTriggeredEnd(wintype)
self.MapWin = wintype
end
[/lua]

has wintype, but that isn’t even in the actual init.lua file. I will search for this variable, for now see if you can find out what to do with the above, as I have a feeling that will work.

I actually don’t get anything. I only got this:
RunConsoleCommand blocked - sent before player spawned (ZLib_Installed)
TTT: The Traitors have been defeated!
Round state: 4
TTT: Let’s look at the round report for 30 seconds.
I doubt that’s anything. Was that what you’re looking for?

He wanted to know if you were actually recieving a … (can’t think of the word) … value ? (I guess that works). And since you aren’t recieving a value it isn’t printing, so you need to use the info I gave above, or find another way.

I actually have no idea what to do with that, and I am really sorry lol. What you wrote actually makes sense, but I have no knowledge of what to do with it :frowning:

In your file put this here and try this, with your table of course.

[lua]
function EndRound( type )
if type == WIN_INNOCENT then BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])
elseif type == WIN_TRAITOR then BroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])
elseif type == WIN_TIMELIMIT thenBroadcastLua([[surface.PlaySound(]]…table.Random(music)…[[)]])
end
end
[/lua]

I copied what you said and put it into my script, and nothing came up. Could you perhaps paste it into the script? I don’t think I’m placing it right. And thanks for all your help so far :slight_smile: I appreciate it.
I’m placing the script in autorun>server btw.