• Noise when a certain player joins the server
    37 replies, posted
Hi, I was wondering if there was a way on a server that when a certain person or a certain group member joins a sound will play throughout the server ? Let me explain in detail: So one of my friends Brain joins the server, A sound is played for him joining and the server is now aware he has joined....but when a normal player joins the sound for people joining is played. If this is possible could someone please assist me in this or tell me how it works / how to do it. Thanks in advance,
All you need: Hook: [url]http://wiki.garrysmod.com/page/GM/PlayerInitialSpawn[/url] Func to play sound: [url]http://wiki.garrysmod.com/page/surface/PlaySound[/url] Then [url]http://wiki.garrysmod.com/page/Category:net[/url] or [url]http://wiki.garrysmod.com/page/Player/SendLua[/url]
Excuse my ignorance, but where might I put the functions and hook in my server structure ? I am using darkrp
So you just want a finished code?
[QUOTE=Netheous;45241179]So you just want a finished code?[/QUOTE] I really don't want to bother you with doing the code for me, I would do the code myself if told where to put it. but if you decide to do it for me I would be very thankful....it's up to you man :)
[QUOTE=BeZerk;45241189]I really don't want to bother you with doing the code for me, I would do the code myself if told where to put it. but if you decide to do it for me I would be very thankful....it's up to you man :)[/QUOTE] lua/autorun/ will be fine, just make a file there and name it whatever. Remember to use "if SERVER then" and "if CLIENT then" to separate server and client if you decide to use one file.
[QUOTE=Netheous;45241200]lua/autorun/ will be fine, just make a file there and name it whatever. Remember to use "if SERVER then" and "if CLIENT then" to separate server and client if you decide to use one file.[/QUOTE] Thanks man, I'll give it a try.
[QUOTE=BeZerk;45241212]Thanks man, I'll give it a try.[/QUOTE] No problem, post below if anything.
[QUOTE=Netheous;45241229]No problem, post below if anything.[/QUOTE] Sorry for being a pest, Do I put the "if SERVER" part before or after the hook and functions ? [editline]29th June 2014[/editline] [QUOTE=Netheous;45241229]No problem, post below if anything.[/QUOTE] And also where do I put "if Client"
You only need the client-side stuff if you're using surface.PlaySound.
[QUOTE=code_gs;45241258]You only need the client-side stuff if you're using surface.PlaySound.[/QUOTE] Thanks for the help! Do I just put the hook and functions inside the if statement or outside it ?
[QUOTE=BeZerk;45241276]Thanks for the help! Do I just put the hook and functions inside the if statement or outside it ?[/QUOTE] Inside the if SERVER then statement.
[QUOTE=code_gs;45241353]Inside the if SERVER then statement.[/QUOTE] Could you please write out the code for me, there is no example for the last string.
[QUOTE=BeZerk;45241402]Could you please write out the code for me, there is no example for the last string.[/QUOTE] [lua] if SERVER then -- serverside shit here end if CLIENT then -- clientside here end [/lua]
[QUOTE=StonedPenguin;45241499][lua] if SERVER then -- serverside shit here end if CLIENT then -- clientside here end [/lua][/QUOTE] Thanks For the example, if i'm not mistaken it would look like this ?: [lua]if SERVER then surface.PlaySound( string soundfile ) GM:PlayerInitialSpawn( Player player ) end if CLIENT then Player:SendLua( string script ) end[/lua]
Here's an example using net :: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_zipped_code/acecool_ttt_end_round.rar[/url] Here's the same thing, except it has the audio files to show where to put them: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_zipped_code/acecool_ttt_end_round_music.rar[/url] The only difference is you'd have to change the TTT End Round hook name to PlayerInitialSpawn and enable a check to see if it is the "special" player, if so then proceed or return; If you only want the sound to go to THAT player, you'd have to use net.Send( Player ) instead of net.Broadcast( );...
[QUOTE=Acecool;45241801]Here's an example using net :: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_zipped_code/acecool_ttt_end_round.rar[/url] Here's the same thing, except it has the audio files to show where to put them: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_zipped_code/acecool_ttt_end_round_music.rar[/url] The only difference is you'd have to change the TTT End Round hook name to PlayerInitialSpawn and enable a check to see if it is the "special" player, if so then proceed or return; If you only want the sound to go to THAT player, you'd have to use net.Send( Player ) instead of net.Broadcast( );...[/QUOTE] I am now so confused, I have no idea what the check code is or how to change the hook for end round :/ Sorry for being a pest :P
The server file has 3 arrays containing references to sounds: END_OF_ROUND_WIN_INNOCENT_SOUNDS END_OF_ROUND_WIN_TRAITOR_SOUNDS END_OF_ROUND_WIN_TIMELIMIT_SOUNDS A reference to a default sound: END_OF_ROUND_WIN_DEFAULT_SOUND The network string ( needs to be initialized to send net message ) which must match with the net.Start( x ), and the net.Receive( x, ...: util.AddNetworkString( "_ttt_end_round_music" ); 3 loops which add the sound resources so they can be downloaded: for k, v in pairs... for k, v in pairs... for k, v in pairs... function which is referenced in the hook: local function endofround( wintype ) hook.Add( "TTTEndRound", "Acecool:TTTEndRoundMusic", endofround ) Basically, you'd change TTTEndRound to PlayerInitialSpawn Next, you can rename wintype to ply if you want; you don't have to though; if you do you'd need to change all other instances of wintype in that function. Next, I'd recommend deleting 2 of the arrays ( and all references, including in the function, the resource.AddFile, etc ) with sounds, leave one, or if you only want 1 sound use the default sound. Delete references to everything else. If you want sounds for specific people, you can set the key of the arrays to steamid or other identifiable marks for the player. Make sure the code matches up and done. What do you end up with? If you ended up with something like this from modifying the server file, you're close to the instructions with a little deviation.. See how much shorter it is? Oh, since we didn't rename the network string, the client file doesn't need to be changed. [code]// // sv_end_round_music.lua by Josh 'Acecool' Moser // // // Define your sounds here, follow the pattern // SPECIAL_PLAYER_SOUNDS = { ["STEAM_0:1:4173055"] = "round/sound1.mp3"; -- Acecool ["STEAM_0:0:1234"] = "round/sound2.mp3"; -- Unknown ["STEAM_0:1:4321"] = "round/sound3.mp3"; -- Unknown }; if ( SERVER ) then // The very important message name... util.AddNetworkString( "_ttt_end_round_music" ); // Make sure they're added so the client can download them for k, v in pairs( SPECIAL_PLAYER_SOUNDS ) do resource.AddFile( "sound/" .. v ); end hook.Add( "PlayerInitialSpawn", "Acecool:WhateverNameIWantToGiveIt", function( _p ) // Default sound local _defaultsound = SPECIAL_PLAYER_DEFAULT_SOUND // Shorter local _steam = _p:SteamID( ); // Shorter local _sound = SPECIAL_PLAYER_SOUNDS[ _steam ]; // Stop, don't send if _sound isn't set ( or use a default sound for all players... ) if ( !_sound ) then return; end // Send the package... net.Start( "_ttt_end_round_music" ); net.WriteString( _sound || _defaultsound ); -- This either sends _sound, or if not set then _default sound... We stopped above with if !_sound then return.. Comment that line to use default sound for all players, or special for specific players. net.Broadcast( ); end ); end[/code]
[QUOTE=Acecool;45242008]The server file has 3 arrays containing references to sounds: END_OF_ROUND_WIN_INNOCENT_SOUNDS END_OF_ROUND_WIN_TRAITOR_SOUNDS END_OF_ROUND_WIN_TIMELIMIT_SOUNDS A reference to a default sound: END_OF_ROUND_WIN_DEFAULT_SOUND The network string ( needs to be initialized to send net message ) which must match with the net.Start( x ), and the net.Receive( x, ...: util.AddNetworkString( "_ttt_end_round_music" ); 3 loops which add the sound resources so they can be downloaded: for k, v in pairs... for k, v in pairs... for k, v in pairs... function which is referenced in the hook: local function endofround( wintype ) hook.Add( "TTTEndRound", "Acecool:TTTEndRoundMusic", endofround ) Basically, you'd change TTTEndRound to PlayerInitialSpawn Next, you can rename wintype to ply if you want; you don't have to though; if you do you'd need to change all other instances of wintype in that function. Next, I'd recommend deleting 2 of the arrays ( and all references, including in the function, the resource.AddFile, etc ) with sounds, leave one, or if you only want 1 sound use the default sound. Delete references to everything else. If you want sounds for specific people, you can set the key of the arrays to steamid or other identifiable marks for the player. Make sure the code matches up and done. What do you end up with? If you ended up with something like this from modifying the server file, you're close to the instructions with a little deviation.. See how much shorter it is? Oh, since we didn't rename the network string, the client file doesn't need to be changed. [code]// // sv_end_round_music.lua by Josh 'Acecool' Moser // // // Define your sounds here, follow the pattern // SPECIAL_PLAYER_SOUNDS = { ["STEAM_0:1:4173055"] = "round/sound1.mp3"; -- Acecool ["STEAM_0:0:1234"] = "round/sound2.mp3"; -- Unknown ["STEAM_0:1:4321"] = "round/sound3.mp3"; -- Unknown }; if ( SERVER ) then // The very important message name... util.AddNetworkString( "_ttt_end_round_music" ); // Make sure they're added so the client can download them for k, v in pairs( SPECIAL_PLAYER_SOUNDS ) do resource.AddFile( "sound/" .. v ); end hook.Add( "PlayerInitialSpawn", "Acecool:WhateverNameIWantToGiveIt", function( _p ) // Default sound local _defaultsound = SPECIAL_PLAYER_DEFAULT_SOUND // Shorter local _steam = _p:SteamID( ); // Shorter local _sound = SPECIAL_PLAYER_SOUNDS[ _steam ]; // Stop, don't send if _sound isn't set ( or use a default sound for all players... ) if ( !_sound ) then return; end // Send the package... net.Start( "_ttt_end_round_music" ); net.WriteString( _sound || _defaultsound ); -- This either sends _sound, or if not set then _default sound... We stopped above with if !_sound then return.. Comment that line to use default sound for all players, or special for specific players. net.Broadcast( ); end ); end[/code][/QUOTE] Woah, thanks man. So the code at the end is finished ? and where do I put this code....I understand what your saying about the example steam names at the top of the code EDIT: [URL="http://gyazo.com/55b61b0838953c1fb4ac5ab8e4d277b8"]http://gyazo.com/55b61b0838953c1fb4ac5ab8e4d277b8[/URL] Woah sorry if some of us ain't so code savvy.
As a rule of thumb, if you see any non-SWEP code with if SERVER then or if CLIENT then in it, most of the time, it will go in lua/autorun.
[QUOTE=code_gs;45242906]As a rule of thumb, if you see any non-SWEP code with if SERVER then or if CLIENT then in it, most of the time, it will go in lua/autorun.[/QUOTE] thanks for the help, what about sound files then...would I add them into a folder in autorun.
[QUOTE=BeZerk;45242941]thanks for the help, what about sound files then...would I add them into a folder in autorun.[/QUOTE] Put them in garrysmod/sound/(any subdirectories after)
Alright man, thanks for your help. Is there anything within the code that was provided I should change ?
Besides the SteamID's and sounds? No, but you can change the hook name if you wish.
[QUOTE=code_gs;45243055]Besides the SteamID's and sounds? No, but you can change the hook name if you wish.[/QUOTE] Thanks for the help, The people that helped in this thread are the nicest people I have met on facepunch! Wish the rest could be like this ;) *Looks to Gamemodes and Releases section* [editline]29th June 2014[/editline] [QUOTE=code_gs;45243055]Besides the SteamID's and sounds? No, but you can change the hook name if you wish.[/QUOTE] Nothing is happening when I join the server....I put the correct SteamID and left the functions alone....I changed the sound directories to where they are on the server and droped the mp3 files where they should go.....help :p
It could be that the player isn't fully in game when the sound plays... You could do something in the client file to ensure they're fully connected by doing: [lua]net.Receive( "message_name", function( len ) local _sound = net.ReadString( ); // Add a hook which we know only runs when player is not in console and is IN GAME! hook.Add( "HUDPaint", "CheapWayToEnsurePlayerHearsSound", function( ) // Ensure the player is in the server and valid or wait if ( !IsValid( LocalPlayer( ) ) ) then return; end // Play the sound surface.PlaySound( _sound ); // Remove the HUDPaint hook since it only needed to happen once hook.Remove( "HUDPaint", "CheapWayToEnsurePlayerHearsSound" ); end ); end );[/lua] Since the sound isn't top priority, it doesn't matter if the client takes over with this task. There are ways to detect player fully connected through server but requires hook to always run; since this is low priority, it won't matter... -- Edit: Fixed typo, thanks code_gs!
[QUOTE=Acecool;45243673]It could be that the player isn't fully in game when the sound plays... You could do something in the client file to ensure they're fully connected by doing: [lua]net.Receive( "message_name", function( len ) local _sound = net.ReadString( ); // Add a hook which we know only runs when player is not in console and is IN GAME! hook.Add( "HUDPaint", "CheapWayToEnsurePlayerHearsSound", function( ) // Ensure the player is in the server and valid or wait if ( !IsValid( LocalPlayer( ) ) then return; end // Play the sound surface.PlaySound( _sound ); // Remove the HUDPaint hook since it only needed to happen once hook.Remove( "HUDPaint", "CheapWayToEnsurePlayerHearsSound" ); end ); end );[/lua] Since the sound isn't top priority, it doesn't matter if the client takes over with this task. There are ways to detect player fully connected through server but requires hook to always run; since this is low priority, it won't matter...[/QUOTE] So if I put this in the client folder in a lua file it will make the sounds work ? Because I already have the original script in the server folder in lua/autorun
[QUOTE=BeZerk;45243795]So if I put this in the client folder in a lua file it will make the sounds work ? Because I already have the original script in the server folder in lua/autorun[/QUOTE] The original script should be in lua/autorun, as I said earlier. That one should obviously go in client.
[QUOTE=code_gs;45243809]The original script should be in lua/autorun, as I said earlier. That one should obviously go in client.[/QUOTE] Ok moved the original to the autorun folder rather then the server folder, Added the client file into the lua/autorun/client folder and got this error when I joined: [url]http://gyazo.com/d3c4baf05a12f9d8e4af652de7c07135[/url] any ideas ?
Change this: [code] if ( !IsValid( LocalPlayer( ) ) then return; end[/code] to this: [code] if ( !IsValid( LocalPlayer( ) ) ) then return; end[/code]
Sorry, you need to Log In to post a reply to this thread.