TTT Music problem


I have this here script that is supposed to randomly select one of the songs listed in the script then play them at the start of each TTT round. Unfortunately I get an error when this happens.


hook.Add("TTTBeginRound", "play sounds!", function()
local StartSounds = {}
StartSounds[1] = "round/skyrimstart.mp3"
StartSounds[2] = "round/skyrimstart.mp3"
StartSounds[3] = "round/skyrimstart.mp3"

local index = math.random(1,3)

That script is in autorun/client.


[ERROR] lua/autorun/client/tttstartsound.lua:8: attempt to index global 'StartSound' (a nil value)
  1. fn - lua/autorun/client/tttstartsound.lua:8
   2. Call - addons/ulib/lua/ulib/shared/hook.lua:183
    3. RoundStateChange - gamemodes/terrortown/gamemode/cl_init.lua:138
     4. func - gamemodes/terrortown/gamemode/cl_init.lua:214
      5. unknown - lua/includes/modules/net.lua:31

I’m a pretty amateur lua guy here and don’t understand how to fix this problem or what is wrong with the code. I also have a script in autorun/server that looks like this:


Help is appreciated. Thanks

If your variable is StartSounds, why did you put StartSound?

You spelled your tables name incorrectly.

Put your table outside of the hook as well so it isn’t created on every single round start.

[editline]12th March 2014[/editline]

local StartSounds = {
“round/skyrimstart.mp3”, – same thing as saying [1] =

hook.Add(“TTTBeginRound”, “TTTBeginRound.StartSounds”, function()
surface.PlaySound(table.Random(StartSounds)) --you can use table.Random when you have a hash table (counts up from 1,2,3…)

D’oh. Epic fail on my end.

Alright thanks a lot for this. Appears to be working, but I’m getting an error that it’s missing from the disk. I’ll solve that from here thanks.