Admin theme tunes

I’ve been trying to create a script for our server so when a certain steamid spawns for the first time on the server, a set sound file plays (randomly from a table). This is what I got so far, and it should apparently work according to a good lua scripter friend of mine:

[lua] musicz = {}
musicz[1] = “sound/adminthemes/h2g2theme.mp3”

function PlayerJoin( ply )
–Megamaff
if ply:SteamID( ) == “STEAM_0:1:16013977” then

for _, p in pairs( player.GetAll( ) )

p:EmitSound( table.Random(musicz) );

end

end

end
hook.Add( “PlayerInitialSpawn”, “PlayerJoin”, PlayerJoin)
[/lua]

However, it does not work :frowning: Any ideas how to get this working?

Your for loop is wrong it should be:

[lua]
for _, p in pairs(player.GetAll()) do

end
[/lua]

Also emitting on every player is bad(It will sound bad at least if youre close.). You should either only emit it on the player who joined or use surface.PlaySound on every player. Also why 3 end? You only need 2 here.

Lol thanks. Ill try it now.

One for the fuction, one for the if statement and one for the loop.

Try this instead:

[lua]local steamids = {
“STEAM_0:1:16013977”,
}

local sounds = {
“sound/adminthemes/h2g2theme.mp3”,
}

hook.Add(“PlayerInitialSpawn”,“PlayerJoin”,function(ply)
for _,v in ipairs(steamids) do if ply:SteamID() == v then
for _,p in ipairs(player.GetAll()) do p:SendLua(“surface.PlaySound(”"…table.Random(sounds)…"")") end
end end
end)[/lua]

That way, you just need to put the steamids and sounds in a table. Also, instead of emitting sound it plays it from the client.

edit Or you could do it so that there was a specific theme corresponding to each admin, like this:

[lua]local steamids = {
{“STEAM_0:1:16013977”,“sound/adminthemes/h2g2theme.mp3”},
}

hook.Add(“PlayerInitialSpawn”,“PlayerJoin”,function(ply)
for _,v in ipairs(steamids) do if ply:SteamID() == v[1] then
for _,p in ipairs(player.GetAll()) do v:SendLua(“surface.PlaySound(”"…v[2]…"")") end
end end
end)[/lua]

You just put a table inside steamids containing the ID and the sound path, as so.

I really like this method most, but is it possible to have a number of sounds (a set picked by the certain admin for them only) randomly selected each time they join and spawn for the first time?

Yeah, it would look like this:

[lua]local steamids = {
{“STEAM_0:1:16013977”,{
“sound/adminthemes/h2g2theme.mp3”,
“sound/adminthemes/lolwut.mp3”,
},
},
{“STEAM_0:1:LOLWUT”,{
“theme.mp3”,
“theme.wav”,
},
},

}

hook.Add(“PlayerInitialSpawn”,“PlayerJoin”,function(ply)
for _,v in ipairs(steamids) do if ply:SteamID() == v[1] then
for _,p in ipairs(player.GetAll()) do p:SendLua(“surface.PlaySound(”"…table.Random(v[2])…"")") end
end end
end)[/lua]

Basically, there’s an overall table. In this table there are subtables containing the steamid and a sub-subtable containing the many sounds. It can be a little tough to place the brackets in the right place, but that should work for ya. And make sure you put commas after pretty much everything.

And for the record, this plays a different/random sound per player. Do you want all players to hear the same sound, but still have it randomized?

Why not use the steamid as the key.
[lua]local steamids = {
“STEAM_0:1:16013977” = {
“sound/adminthemes/h2g2theme.mp3”,
“sound/adminthemes/lolwut.mp3”,
},
“STEAM_0:1:LOLWUT”, = {
“theme.mp3”,
“theme.wav”,
},
}

hook.Add(“PlayerInitialSpawn”,“PlayerJoin”,function(ply)
for k,v in ipairs(steamids) do
if ply:SteamID() == k then
for _,p in ipairs(player.GetAll()) do
p:SendLua(“surface.PlaySound(”"…table.Random(v)…"")")
end
end
end
end)[/lua]

Sure that works. But I put a typo in there that’s in your code as well; it should be p:SendLua(), not v.

Yes, you are right. Edited

I was hoping to have all hear the same one yet still randomised :stuck_out_tongue: That could still work though!

This makes every player hear the same sound. [lua]local steamids = {
“STEAM_0:1:16013977” = {
“sound/adminthemes/h2g2theme.mp3”,
“sound/adminthemes/lolwut.mp3”,
},
“STEAM_0:1:LOLWUT”, = {
“theme.mp3”,
“theme.wav”,
},
}

hook.Add(“PlayerInitialSpawn”,“PlayerJoin”,function(ply)
for k,v in ipairs(steamids) do
if ply:SteamID() == k then
local r = table.Random(v);
for _,p in ipairs(player.GetAll()) do
p:SendLua(“surface.PlaySound(”"…r…"")")
end
end
end
end)[/lua]

I got one that works now thanks!! Final problem is getting people to download the sounds from the server automatically. It doesnt automatically download on its own but I know theres a way with ulx, but not sure how. Any help?

[lua]resource.AddFile(“filenamehere”)[/lua]

EDIT!!!
Ok, I have fixed the problems and have finally got a culprit for our last problem. When trying to download the sounds from the test server, everything works perfectly! Yet! On the server we need it on, it seems to download but doesn’t work. We think it doesnt save it to the correct location or doesn’t download at all. Any ideas why?

So MakeR’s script worked?

No, yours did though. No errors or anything. Only problem is everyone hears a random song from that specific selection. So 3 people will hear random things each from the persons specified playlist.
Heres the code:
[lua]
resource.AddFile(“sound/adminthemes/Mwakeup.mp3”)
resource.AddFile(“sound/adminthemes/h2g2theme.mp3”)
resource.AddFile(“sound/adminthemes/cres.mp3”)
resource.AddFile(“sound/adminthemes/cstewie.mp3”)
resource.AddFile(“sound/adminthemes/quag.mp3”)
resource.AddFile(“sound/adminthemes/termytheme.mp3”)
resource.AddFile(“adminthemes/Mwakeup.mp3”)
resource.AddFile(“adminthemes/h2g2theme.mp3”)
resource.AddFile(“adminthemes/cres.mp3”)
resource.AddFile(“adminthemes/cstewie.mp3”)
resource.AddFile(“adminthemes/quag.mp3”)
resource.AddFile(“adminthemes/termytheme.mp3”)

musicm = {}
musicm[1] = “adminthemes/h2g2theme.mp3”
musicm[2] = “adminthemes/termytheme.mp3”
musicc = {}
musicc[1] = “adminthemes/Cres.mp3”
musicc[2] = “adminthemes/mwakeup.mp3”
musicc[3] = “adminthemes/cstewie.mp3”

musicv = {}
musicv[1] = “adminthemes/quag.mp3”

function PlayerJoin( ply )

if ply:SteamID( ) == "STEAM_0:1:16013977" then 

	for _, p in pairs( player.GetAll( ) ) do

	p:EmitSound( table.Random(musicm) );
end

end

if ply:SteamID() == "STEAM_0:0:4457836" then

	for _, p in pairs( player.GetAll( ) ) do
	
	p:EmitSound( table.Random(musicc) );
end

end

if ply:SteamID() == "STEAM_0:0:18245113" then

	for _, p in pairs( player.GetAll( ) ) do
	
	p:EmitSound( table.Random(musicv) );
	end
end	

end

hook.Add( “PlayerInitialSpawn”, “PlayerJoin”, PlayerJoin)[/lua]

Erm, try this, it’s a lot easier…

[lua]local songs = {
“STEAM_0:1:16013977” = {
“adminthemes/h2g2theme.mp3”,
“adminthemes/termytheme.mp3”,
},
“STEAM_0:0:4457836” = {
“adminthemes/Cres.mp3”,
“adminthemes/mwakeup.mp3”,
“adminthemes/cstewie.mp3”,
},
“STEAM_0:0:18245113” = {
“adminthemes/quag.mp3”,
},
}

for _,v in pairs(songs) do for _,q in pairs(v) do resource.AddFile(q) end end

hook.Add(“PlayerInitialSpawn”,“PlayerJoin”,function(ply)
for k,v in pairs(songs) do
if k == ply:SteamID() then
local r = table.Random(v)
for _,p in ipairs(player.GetAll()) do p:SendLua(“surface.PlaySound(”"…r…"")") end
end
end
end)
[/lua]

Adds the files without you having to list them, and makes it easier to set up the tables and stuff.

Hey i haev this is lua/autorun
[lua]
resource.AddFile(“adminthemes/virustheme.mp3”)

local songs = {
“STEAM_0:1:18355339” = {
“adminthemes/virustheme.mp3”,
}

for _,v in pairs(songs) do for _,q in pairs(v) do resource.AddFile(q) end end

hook.Add(“PlayerInitialSpawn”,“PlayerJoin”,function(ply)
for k,v in pairs(songs) do
if k == ply:SteamID() then
local r = table.Random(v)
for _,p in ipairs(<span class=“highlight”>player</span>.GetAll()) do p:SendLua(“surface.PlaySound(”"…r…"")") end
end
end
end)
[/lua]

and i have virustheme.mp3 in garrysmod/adminthemes and it dosent work

First: you don’t need to do resource.AddFIle, as the line below the songs table does it for you.

Second, you didn’t close the songs table. It needs another bracket at the end.

Third, it goes in /lua/autorun/server.

Fourth, this stuff should be relative to the sound directory. It should be /garrysmod/sound/adminthemes/virustheme.mp3 (also I made a mistake in the resource.AddFile part, so use this:)

[lua]local songs = {
“STEAM_0:1:18355339” = {
“adminthemes/virustheme.mp3”,
},
}

for _,v in pairs(songs) do for _,q in pairs(v) do resource.AddFile("…/sounds/"…q) end end

hook.Add(“PlayerInitialSpawn”,“PlayerJoin”,function(ply)
for k,v in pairs(songs) do
if k == ply:SteamID() then
local r = table.Random(v)
for _,p in ipairs(player.GetAll()) do p:SendLua(“surface.PlaySound(”"…r…"")") end
end
end
end)
[/lua]

So if the sound path is /garrysmod/sound/adminthemes/virustheme.mp3 then the path in the “songs” table is still adminthemes/virustheme.mp3.