TTT Random music at the end of the round script not working.
5 replies, posted
The script that I made that isn't working is below.
[B]shared.lua[/B]
____________________________________________________
EndGameSounds = {}
EndGameSounds[1] = "sound/roundendmusic01.wav"
EndGameSounds[2] = "sound/roundendmusic02.wav"
EndGameSounds[4] = "sound/roundendmusic04.wav"
EndGameSounds[5] = "sound/roundendmusic05.wav"
EndGameSounds[6] = "sound/roundendmusic06.wav"
EndGameSounds[7] = "sound/roundendmusic07.wav"
EndGameSounds[8] = "sound/roundendmusic08.wav"
EndGameSounds[9] = "sound/roundendmusic00.wav"
EndGameSounds[10] = "sound/roundendmusic10.wav"
resource.AddFile("sound/roundendmusic01.wav")
resource.AddFile("sound/roundendmusic02.wav")
resource.AddFile("sound/roundendmusic03.wav")
resource.AddFile("sound/roundendmusic04.wav")
resource.AddFile("sound/roundendmusic05.wav")
resource.AddFile("sound/roundendmusic06.wav")
resource.AddFile("sound/roundendmusic07.wav")
resource.AddFile("sound/roundendmusic08.wav")
resource.AddFile("sound/roundendmusic09.wav")
resource.AddFile("sound/roundendmusic10.wav")
________________________
[B]RoundEndMusic.lua[/B]
_______________________
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )
function PlayMusic()
BroadcastLua('surface.PlaySound( EndGameSounds[ math.Random( 1, 10 ) ] )')
end
hook.Add( TTTEndRound, "PlayMusic", PlayMusic )
_____________________
This script shows no errors but doesn't work. Can someone help me?
There should be quotes around TTTEndRound, you did do that correct?
Also you shouldn't use BroadcastLua, use the network library.
[url]http://wiki.garrysmod.com/page/Net_Library_Usage[/url]
I can't seem to figure out how to send script like "sound.Play" to client. The page tells me how to send tables and strings and integers to the client. I wonder if this would work:
local MusicPicker = math.Random( 1, 10 )
function PlayMusic()
if MusicPicker == 1 then
surface.PlaySound( "roundendmusic01" )
elseif musicpicker == 2 then
surface.PlaySound( "roundendmusic02" )
// and so on
Alright guys this is my new script. Still Causing Problems.
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )
//serverside stuff
hook.Add( "TTTEndRound", "SendMusic", SendMusic )
if ( server ) then
util.AddNetWorkString( "ChooseMusic" )
local MusicInteger = math.random(1, 10)
function SendMusic()
net.Start( "ChooseMusic" )
net.WriteInt( MusicInteger )
net.Broadcast()
end
end
//client stuff
if ( client ) then
net.Receive( "ChooseMusic", function(len)
if net.ReadInt() == 1 then
surface.PlaySound( "roundendmusic01.wav" )
elseif net.ReadInt() == 2 then
surface.PlaySound( "roundendmusic02.wav" )
elseif net.ReadInt() == 3 then
surface.PlaySound( "roundendmusic03.wav" )
elseif net.ReadInt() == 4 then
surface.PlaySound( "roundendmusic04.wav" )
elseif net.ReadInt() == 5 then
surface.PlaySound( "roundendmusic05.wav" )
elseif net.ReadInt() == 6 then
surface.PlaySound( "roundendmusic06.wav" )
elseif net.ReadInt() == 7 then
surface.PlaySound( "roundendmusic07.wav" )
elseif net.ReadInt() == 8 then
surface.PlaySound( "roundendmusic08.wav" )
elseif net.ReadInt() == 9 then
surface.PlaySound( "roundendmusic09.wav" )
elseif net.ReadInt() == 10 then
surface.PlaySound( "roundendmusic10.wav" )
end
end )
end
___________________
I am A beginner with Gmod's LUA coding if you can't already tell.
[lua]
if SERVER then
--- Traitor
local T = {}
for k, v in pairs(file.Find("sound/trait/*", "GAME")) do
resource.AddFile(v)
table.Insert(T, v)
end)
--- Innocent
local I = {}
for k, v in pairs(file.Find("sound/inno/*", "GAME")) do
resource.AddFile(v)
table.Insert(I, v)
end)
--- Times Up
local Ti = {}
for k, v in pairs(file.Find("sound/time/*", "GAME")) do
resource.AddFile(v)
table.Insert(Ti, v)
end)
local function RandomMusic(win)
if (win == WIN_TRAITOR) then
lua = [[surface.PlaySound(]]table.Random(T)[[)]]
elseif (win == WIN_TIMELIMIT) then
lua = [[surface.PlaySound(]]table.Random(Ti)[[)]]
elseif (win == WIN_INNOCENT) then
lua = [[surface.PlaySound(]]table.Random(I)[[)]]
end
BroadcastLua(lua)
end
hook.Add("TTTEndRound", "RandomMusic", RandomMusic)
end[/lua]
Just use this. Stuff your sounds in
/garrysmod/sound/inno/
/garrysmod/sound/trait/
/garrysmod/sound/time/
and it will automatically cache them/send them. It's so much less hassle then editing a lua file.
Sorry, you need to Log In to post a reply to this thread.