Start Round Music

Hello, I want to modify this code


-- You can add up to 3 sounds for this. Add or delete resource.addfile as you need
resource.AddFile("sound/startandendmusic/startmusic/start_music_1.mp3")
resource.AddFile("sound/startandendmusic/startmusic/start_music_2.mp3")

-- Remember to change the name of the sounds to the sound you want from above
local function PlayMusic(wintype)
   if wintype == WIN_INNOCENT then
      BroadcastLua('surface.PlaySound("music/start/start_music_1.mp3")')

   elseif wintype == WIN_TRAITOR then
      BroadcastLua('surface.PlaySound("music/start/start_music_2.mp3")')
   end
end
hook.Add("TTTBeginRound", "MyMusic", PlayMusic)

But instead of round end I want it to randomize one of these files and play it at the beginning.

THanks in advanced.

Just a note … as I remember it “TTTBeginRound” has no parameters.
I’m tired, its 2:46 here … and no clue if this works.
[lua]
// List over all the sound files (Without the ‘sound/’ !!!)
local List = {} // Make table
List[1] = “startandendmusic/startmusic/start_music_1.mp3” // Add to table
List[2] = “startandendmusic/startmusic/rickroll.mp3”
List[3] = “startandendmusic/startmusic/getonmyhorse.mp3”

// We make the clients download all the music in the list
for i, fil in ipairs(List) do // Runs through the list
resource.AddFile(“sound/”…fil) // Adds them to the “download” list
end

local function PlayMusic()
BroadcastLua(‘surface.PlaySound("’…List[math.random(1,#List)]…’")’) // Run this string with a random sound path from the list
end
hook.Add(“TTTBeginRound”, “SomethingThatArentTaken”, PlayMusic) // Runs PlayMusic each time the round starts[/lua]
No idea if I did a mistype and if I were you, I won’t use the “BroadcastLua” that much. Night

Hmm, it doesn’t work. It’s not forcing us to download anything and there are no errors.

This is my current code


-- List over all the sound files (Without the 'sound/' !!!)
local List = {} // Make table
List[1] = "music/start/track1.mp3" // Add to table 
List[2] = "music/start/track2.mp3"

local function PlayMusic()
    BroadcastLua('surface.PlaySound("'..List[math.random(1,#List)]..'")') -- Run this string with a random sound path from the list
end
hook.Add("TTTBeginRound", "SomethingThatArentTaken", PlayMusic) -- Runs PlayMusic each time the round starts

But on round start I get this error


Failed to load sound "music\start	rack2.mp3", file probably missing from disk/repository
Y

The file that forces them to download is a seperate file, but this is the code


if (SERVER) then
    resource.AddFile("sound/music/start/track1.mp3")
    resource.AddFile("sound/music/start/track2.mp3")
end


Maybe because you forgot to copy this?:
[lua]
// We make the clients download all the music in the list
for i, fil in ipairs(List) do // Runs through the list
resource.AddFile(“sound/”…fil) // Adds them to the “download” list
end[/lua]

Also, when you put // do you mean --?

Because when you put // it still counts it as code, but with – it doesn’t.

Probably your editor which doesn’t like // being comments, they’re not in default lua, it’s something garry added.

Trust me, I only added those to help you continue working with G-LUA. If you knew that ‘//’ kinda looks like ‘–’ … you should have seen the connection :stuck_out_tongue:
[Protip] Read the full posted code … sometimes it pays off reading the “comments” that someone spent 10-20 key-hits on.

Get notepad++ (A great tool) and the Gmod plugin for it: http://code.google.com/p/npp-gmod-lua/

Ok, it’s nearly all working now.

Only one problem, it’s only playing one song, and that’s strack1. Any idea why this is happening?

This is the code:


-- List over all the sound files (Without the 'sound/' !!!)
local List = {} // Make table
List[1] = "strack1.mp3" -- Add to table 
List[2] = "strack1.mp3"
 
-- We make the clients download all the music in the list
for i, fil in ipairs(List) do -- Runs through the list
    resource.AddFile("sound/"..fil) -- Adds them to the "download" list
end
 
local function PlayMusic()
    BroadcastLua('surface.PlaySound("'..List[math.random(1,#List)]..'")') -- Run this string with a random sound path from the list
end
hook.Add("TTTBeginRound", "SomethingThatArentTaken", PlayMusic) -- Runs PlayMusic each time the round starts

Oh, and I already use Notepad++ and the Plugin wouldn’t install correctly.

From my LUA knowledge (Extremely Small) there doesn’t seem to be anything wrong with the code.

[editline]23rd February 2013[/editline]

Wait, forget all that. I just realized I derped hard xD

What have the symbol “/” ever don to you? .-.
You can’t just dis’ it cu’z its different!

Jk :stuck_out_tongue: