End of Round Music Addon Lua error

Hello. I am new to facepunch and I need some help with my addon. I am getting a lua error in the console for my server. Here it is.


local soundTable ={Inno={"america.mp3", "gta.mp3, "pokemon.mp3", "riddle.mp3", "silence.mp3", "wake.mp3", "years.mp3"}, Trait={"america.mp3", "gta.mp3, "pokemon.mp3", "riddle.mp3", "silence.mp3", "wake.mp3", "years.mp3"}, Time={"america.mp3", "gta.mp3, "pokemon.mp3", "riddle.mp3", "silence.mp3", "wake.mp3", "years.mp3"}, "wake.mp3", "years.mp3"}}

local function PlayMusic(wintype)
	if wintype == WIN_INNOCENT then
		BroadcastLua('surface.PlaySound("'..table.Random(soundTable[Inno])..'")')
	elseif wintype == WIN_TRAITOR then
		BroadcastLua('surface.PlaySound("'..table.Random(soundTable[Trait])..'")')
	elseif wintype == WIN_TIMELIMIT then
		BroadcastLua('surface.PlaySound("'..table.Random(soundTable[Time])..'")')
	end
end
hook.Add("TTTEndRound", "MyMusic", PlayMusic)

So I am getting this error.

[ERROR] addons/eormusic/lua/autorun/eormusic.lua:1: ‘}’ expected near ‘pokemon’

  1. unknown - addons/eormusic/lua/autorun/eormusic.lua:0

Please help me! Thanks in advance :smile:

put the code inside


 tags

You forgot a " after gta.mp3.

If you don’t care what kind of song plays at the end of the round, you can just make the script way smaller, because you’re using the same tracks regardless if it’s traitor, innocent, or a timeout.



local soundTable = { "america.mp3", "gta.mp3", "pokemon.mp3", "riddle.mp3", "silence.mp3", "wake.mp3", "years.mp3" }

hook.Add("TTTEndRound", "MyMusic", function()
    BroadcastLua('surface.PlaySound("'..table.Random(soundTable)..'")')
end)


ok um wow. I did everything u said, but i got this error.


[ERROR] lua/includes/extensions/table.lua:150: bad argument #1 to 'pairs' (table expected, got nil)
  1. pairs - [C]:-1
   2. Count - lua/includes/extensions/table.lua:150
    3. Random - lua/includes/extensions/table.lua:159
     4. fn - addons/eormusic/lua/autorun/eormusic.lua:5
      5. Call - addons/ulib/lua/ulib/shared/hook.lua:179
       6. EndRound - gamemodes/terrortown/gamemode/init.lua:799
        7. unknown - gamemodes/terrortown/gamemode/init.lua:321

Timer Failed! [winchecker][@gamemodes/terrortown/gamemode/init.lua (line 367)]


Post the full script.


local soundTable = { "america.mp3", "gta.mp3", "pokemon.mp3", "riddle.mp3", "silence.mp3", "wake.mp3", "years.mp3" }

hook.Add("TTTEndRound", "MyMusic", function()
    BroadcastLua('surface.PlaySound("'..table.Random(soundTable)..'")')
end)

I changed my code to this.


resource.AddFile("sound/america.mp3")
resource.AddFile("sound/gta.mp3")
resource.AddFile("sound/riddle.mp")
resource.AddFile("sound/silence.mp3")
resource.AddFile("sound/wake.mp3")
resource.AddFile("sound/years.mp3")
resource.AddFile("sound/pokemon.mp3")
local soundTable = {EORmusic={ "america.mp3", "gta.mp3", "pokemon.mp3", "riddle.mp3", "silence.mp3", "wake.mp3", "years.mp3" }}
local function PlayMusic(wintype)
	if wintype == WIN_INNOCENT then
		BroadcastLua('surface.PlaySound("'..table.Random(EORmusic)..'")')
	elseif wintype == WIN_TRAITOR then
		BroadcastLua('surface.PlaySound("'..table.Random(EORmusic)..'")')
	elseif wintype == WIN_TRAITOR then
		BroadcastLua('surface.PlaySound("'..table.Random(EORmusic)..'")')
hook.Add("TTTEndRound", "MyMusic", PlayMusic)
	end
end


But now there is no error in console and no music playing.

That code is filled with so much cancer. The table wasn’t being called correctly and the order of your "end"s etc were misaligned resulting in your hook function never being called.
Here but ill help you out. This was not tested.


resource.AddFile("sound/america.mp3")
resource.AddFile("sound/gta.mp3")
resource.AddFile("sound/riddle.mp")
resource.AddFile("sound/silence.mp3")
resource.AddFile("sound/wake.mp3")
resource.AddFile("sound/years.mp3")
resource.AddFile("sound/pokemon.mp3")
local soundTable = {EORmusic={ "america.mp3", "gta.mp3", "pokemon.mp3", "riddle.mp3", "silence.mp3", "wake.mp3", "years.mp3" }}
local function PlayMusic(wintype)
	if wintype == WIN_INNOCENT then
		BroadcastLua('surface.PlaySound("'..table.Random(soundTable.EORmusic)..'")')
	elseif wintype == WIN_TRAITOR then
		BroadcastLua('surface.PlaySound("'..table.Random(soundTable.EORmusic)..'")')
	elseif wintype == WIN_TRAITOR then
		BroadcastLua('surface.PlaySound("'..table.Random(soundTable.EORmusic)..'")')
	end
end
hook.Add("TTTEndRound", "MyMusic", function(result) PlayMusic(result) end)

A good reference the next time you work with hooks in TTT: http://ttt.badking.net/guides/hooks

Thanks for trying, but there is no error still and still doesnt work. I did what u said.


resource.AddFile("sound/america.mp3")
resource.AddFile("sound/gta.mp3")
resource.AddFile("sound/riddle.mp")
resource.AddFile("sound/silence.mp3")
resource.AddFile("sound/wake.mp3")
resource.AddFile("sound/years.mp3")
resource.AddFile("sound/pokemon.mp3")
local soundTable = {EORmusic={ "america.mp3", "gta.mp3", "pokemon.mp3", "riddle.mp3", "silence.mp3", "wake.mp3", "years.mp3" }}
local function PlayMusic(wintype)
	if wintype == WIN_INNOCENT then
		BroadcastLua('surface.PlaySound("'..table.Random(soundTable.EORmusic)..'")')
	elseif wintype == WIN_TRAITOR then
		BroadcastLua('surface.PlaySound("'..table.Random(soundTable.EORmusic)..'")')
	elseif wintype == WIN_TRAITOR then
		BroadcastLua('surface.PlaySound("'..table.Random(soundTable.EORmusic)..'")')
	end
end
hook.Add("TTTEndRound", "MyMusic", function(result) PlayMusic(result) end)


resource.AddFile( "sound/america.mp3" )
resource.AddFile( "sound/gta.mp3" )
resource.AddFile( "sound/riddle.mp" )
resource.AddFile( "sound/silence.mp3" )
resource.AddFile( "sound/wake.mp3" )
resource.AddFile( "sound/years.mp3" )
resource.AddFile( "sound/pokemon.mp3" )
local soundTable = {
	EORmusic={
		"america.mp3",
		"gta.mp3",
		"pokemon.mp3",
		"riddle.mp3",
		"silence.mp3",
		"wake.mp3",
		"years.mp3"
	}
}

local function PlayMusic( wintype )
	if wintype == WIN_INNOCENT then
		BroadcastLua( string.format( "surface.PlaySound(%u)", table.Random( soundTable.EORmusic ) ) )
	elseif wintype == WIN_TRAITOR then
		BroadcastLua( string.format( "surface.PlaySound(%u)", table.Random( soundTable.EORmusic ) ) )
	end
end
hook.Add( "TTTEndRound", "MyMusic", PlayMusic )

From looking at that I do not directly see a problem. It can be anything. Sadly I do not have a ttt server. So I cant test is correctly. (Yes I know gmod has it by default.)
Your best bet is trying to use net functions instead of BroadcastLua.
What you have to do is debug it. Start at what the server sends to the clients.


local s = string.format( "surface.PlaySound(%u)", table.Random( soundTable.EORmusic ) )
print( s )
BroadcastLua( s )

If that looks fine then go troubleshoot the clients instead. And work your way back to the server. Ask yourself this: Does the sound exist? Is the client getting the command?

Good luck. If you have an error I can help. Without errors I can’t do much.

Why are you bothering with elseif/else statements when all end up playing the same table of sounds

because he just pasted this code from some shit and has no knowledge of lua