Player spawn sounds! HELP.

Hi guys. I have been working massively on my gamemode over the last few days, but I have come across an issue I hope you could help me with.

When a player spawns initally I want to play a sound. But the method I am using plays it so everyone can hear it. I only want the player to hear it.
Here is my code atm, I hope you can help.





local SpawnSounds = { "sao.wav" } 
local music = { "doom.wav" } --Doom music
function GM:PlayerInitialSpawn( ply )--For when the player spawns for the first time



timer.Simple(2, function( 
		ply:EmitSound(table.Random(SpawnSounds),100,100)
		--surface.PlaySound(table.Random(SpawnSounds),100,100)
		--sound.Play(table.Random(SpawnSounds),100,100)
		
	end)

end

[code/]


I also tried some other methods which are commeted out but they didn't work, and caused scriped errors. If you could help me I will be very grateful.

Many thanks

-Duby

Just send a net message to the player as they spawn and play the sound on the receiving end.

Hmmm I see how would you go about doing it? I am quite bad a .net
So seeing a good example will help a lot!

Thanks

Here you go.

But how would I use this with sounds. I am always confused with these .net things.

When you receive the net message on the client, play the sound.

You could also just use this function:

The best and easiest way would be to just use the net library like so…



if SERVER then
	util.AddNetworkString("initialspawnsound")
	hook.Add("PlayerInitialSpawn", "initialspawnsound", function(ply)
		net.Start("initialspawnsound")
		net.Send(ply)
	end)
elseif CLIENT then
	net.Receive("initialspawnsound", function()
		surface.PlaySound("sao.wav")
	end)
end


I have got it working but still everyone on the server can here it.

[editline]19th March 2014[/editline]

With the methodJezzy gave me*

You are sending the message to every player, you need to send it only to the person who just spawned.

So how do I specify that to only the player spawning then? Is the a slight change in syntax?

Thanks.

The hook “PlayerInititalSpawn” has a ply argument and you want to use net.Send(ply) in the hook

Thanks for all your help I have learnt an awful lot from this! I got it working by doing the following. It may not be the most effiecent but it works. ^^

Server:

util.AddNetworkString( “spawn2” )
net.Start(“spawn2”)
net.WriteString(“some text”)
net.Send(ply)

Client:

net.Receive( “spawn2”, function( len, pl )
if ( IsValid( pl ) and pl:IsPlayer() ) then
print( "Message from " … pl:Nick() … " received. Its length is " … tostring( len ) … “.” )

else
	print( "Message from server received. Its length is " .. tostring( len ) .. "." )
	surface.PlaySound("sao.wav")
end

end )

You’ve made it so if the player isn’t valid and they aren’t a player, they play the sound.

Wat :v

I know it seems very confusing to me, but it works.

But how would you do it? Just remove the if and else conditions?

I already posted above how to do it :v:



if SERVER then
	util.AddNetworkString("initialspawnsound")
	hook.Add("PlayerInitialSpawn", "initialspawnsound", function(ply)
		net.Start("initialspawnsound")
		net.Send(ply)
	end)
elseif CLIENT then
	net.Receive("initialspawnsound", function()
		surface.PlaySound("sao.wav")
	end)
end


But it was confusing and is that on the server side or what? I need directions so I can learn from it. Just c and p code doesn’t teach. ^^

It all goes in 1 file, or you can put just


util.AddNetworkString("initialspawnsound")
	hook.Add("PlayerInitialSpawn", "initialspawnsound", function(ply)
		net.Start("initialspawnsound")
		net.Send(ply)
	end)

in a server-sided file and,


net.Receive("initialspawnsound", function()
		surface.PlaySound("sao.wav")
	end)

in a client-sided file. But just stick it in 1 file in lua/autorun

[editline]20th March 2014[/editline]

If you’d like me to explain every line say so and ill go ahead and do it.

I see ok that makes more sense now. So why do you use a hook.add in this case?

So it doesnt overwrite any other code in the PlayerInitialSpawn function, it instead adds on to that code. Now if you’re making a gamemode then you would want to use “function GM:PlayerInitialSpawn()”

Ah this is the case with me. So it would look like this?

**hook.Add(“PlayerInitialSpawn”, “initialspawnsound”,function GM:PlayerInitialSpawn() **