TTT end of round music.

Right so I spent about 80% of my time yesterday trying to automate the adding of songs to our end of round music. There was nothing wrong with the original except to add files I had to edit the lua file to add the path of the song. What I’m trying to accomplish is a drag and drop, after the initial setup of the four folders for the four different win outcomes it should be plain sailing. It was a lot harder than I expected, firstly I found out that the original code was written without taking in to consideration weather it would be run server or client side. So that meant I had to do a whole load of things.

I think its the downloads that are causing my problem, but I can’t see what I’m doing wrong. After putting the files in myself they played without a problem, but when I hadn’t put them in myself my client attempted to download the files but zipped through them and after checking where they should be, they weren’t there. Those who don’t have the files get an error saying:




[ERROR] lua/autorun/endofroundmusic.lua:85: bad argument #1 to 'PlaySound' (string expected, got no value)
  1. PlaySound - [C]:-1
   2. func - lua/autorun/endofroundmusic.lua:85
    3. unknown - lua/includes/modules/net.lua:31




Heres my code.

[LUA]
– WIN_NONE = 1
– WIN_TRAITOR = 2
– WIN_INNOCENT = 3
– WIN_TIMELIMIT = 4

if SERVER then
AddCSLuaFile()
util.AddNetworkString(“EndOfRoundMusicResult”)
end

hook.Add(“Initialize”, “CheckGamemodesEnums”, function()
if SERVER then
game.ConsoleCommand("ttt_cl_soundcues 0
")
end

--Detect files located in the music directories.
EndOfRoundMusic = {}
EndOfRoundMusic[1] = file.Find("sound/win_music_none/*", "GAME")
EndOfRoundMusic[2] = file.Find("sound/win_music_traitor/*", "GAME")
EndOfRoundMusic[3] = file.Find("sound/win_music_innocent/*", "GAME")
EndOfRoundMusic[4] = file.Find("sound/win_music_timeout/*", "GAME")

print("--------------------------------------")
print("--        End of Round Music        --")
print("--------------------------------------")
print("--             No Music             --")
for k, v in ipairs (EndOfRoundMusic[1]) do
	print("--" .. k .. "   " .. v)
end
print("--          Traitor Music           --")
for k, v in ipairs (EndOfRoundMusic[2]) do
	print("--" .. k .. "   " .. v)
end
print("--          Innocent Music          --")
for k, v in ipairs (EndOfRoundMusic[3]) do
	print("--" .. k .. "   " .. v) 
end
print("--          Timeout Music           --")
for k, v in ipairs (EndOfRoundMusic[4]) do
	print("--" .. k .. "   " .. v) 
end
print("--------------------------------------")

--Download sounds and store them in cache ready to be played, then ammend them to contain the correct path.
for k, v in ipairs (EndOfRoundMusic[1]) do
	if SERVER then
		resource.AddSingleFile("sound/win_music_none/"..v)
	end
	util.PrecacheSound("sound/win_music_none/"..v)
	EndOfRoundMusic[1][k] = ("win_music_none/"..v)
end
for k, v in ipairs (EndOfRoundMusic[2]) do
	if SERVER then
		resource.AddSingleFile("sound/win_music_traitor/"..v)
	end
	util.PrecacheSound("sound/win_music_traitor/"..v)
	EndOfRoundMusic[2][k] = ("win_music_traitor/"..v)
end
for k, v in ipairs (EndOfRoundMusic[3]) do
	if SERVER then
		resource.AddSingleFile("sound/win_music_innocent/"..v)
	end
	util.PrecacheSound("sound/win_music_innocent/"..v)
	EndOfRoundMusic[3][k] = ("win_music_innocent/"..v)
end
for k, v in ipairs (EndOfRoundMusic[4]) do
	if SERVER then
		resource.AddSingleFile("sound/win_music_timeout/"..v)
	end
	util.PrecacheSound("sound/win_music_timeout/"..v)
	EndOfRoundMusic[4][k] = ("win_music_timeout/"..v)
end

if SERVER then
	hook.Add("TTTEndRound", "TTTEndRoundMusicc", function(wintype)
		net.Start("EndOfRoundMusicResult")
		net.WriteInt(wintype, 16)
		net.Broadcast()
	end)
else	
	net.Receive("EndOfRoundMusicResult", function(len)
		local wintype = net.ReadInt(16)
		if EndOfRoundMusic[wintype] then
			print(table.Random(EndOfRoundMusic[wintype]))
			surface.PlaySound(table.Random(EndOfRoundMusic[wintype]))
		end
	end)
end

end)
[/LUA]

[editline]20th July 2013[/editline]

Solved: Well I’m glad to say there was nothing wrong with my script, it works just how I intended it to. :slight_smile: just a stupid problem with a folder corruption or something. Fixed now.

I would however be interested in knowing if file.find can be used on just the client or just the server.

Also, with that code each of your players will hear different music. So if one plays longer than the other, or louder than the other and someone tries talking to someone else thinking the music is over, it may not be for them.

I’d recommend doing the randomization on the server-side. Pass wintype and index of song to clients.

You would think so, and I had that suspicion myself, but turns out the same sound will be played on all O.o

The way math.Random works in most, if not all, programming languages is by using time as a seed. Because not every single person will receive it at exactly the same time, there will be fluctuation. Have the clients run console say to speak which sound is playing; I’m sure if you have more than 1 player it will eventually differ.

So far on a server of about 20 everyone hears the same song, but there’s no reason I can’t select the song before hand and send it as a string to the client.

Does the rest of what I’m doing look okay? What is happening when the file.find is run on both the server and the client, and what would be the difference if I only ran it on one?