!=ply:Alive & Stopping Sound

I´ve wrote a Script some weeks ago. It´s working but not perfectly. My Problems:
First: The Sound that is playing works at the first time the script is running. But Everytime after that, the Script gets triggered, the sound stop in the middle.
Second: The Script will be triggered although the script should only be triggered if the player is dead.

Here is the Script I´m talking about:

[lua]if (SERVER) then
util.AddNetworkString(“SuicideMusic”)
util.AddNetworkString(“SuicideMessage”)

local function SuicideBroadcast(tbl)
	net.Start("SuicideMessage")
		net.WriteTable(tbl)
	net.Broadcast()
end

print( "Hook check" )

hook.Add("OnPlayerHitGround", "Suicide", function(ply, inWater, onFloater, speed)
	if inWater and ply:Alive() then return end
	  if speed > 700 then 
	    
	
	        print( "Broadcast check" )
	
	        net.Start("SuicideMusic")
	        net.Send(ply)
	  end   
end)

else
net.Receive(“SuicideMusic”, function(len)
local ply = LocalPlayer()

	ply.Music = CreateSound(ply, "SuperSvenPeterBros.mp3")
	ply.Music:Play()
end)

print( "Music check" )

end[/lua]

Why do I have these problems and how can I solve them ?

P.S.: An If Statement with something like “!=ply:Alive()” didn´t work.

I suggest you go to the Garry’s Mod Wiki and learn basic Lua syntax. To check if the player is dead, you can use


not ply:Alive( )

or


!ply:Alive( )

I would personally recommend using


not

You also should not be storing the music to the player. Just store it as a local variable. Instead of using CreateSound, use

surface.PlaySound. If you would like to manipulate the sound further, you may use CreateSound. However, you do need to stop the sound when it is done by using

CSoundPatch:Stop. Lastly, you should not be returning in the hook. It may prevent others from running. Instead encase the hook with if not ply:Alive( ) and end it at the end of the hook.

EDIT: Why are you checking if they are in water?

You need to stop the sound on the client before you play the sound, just in case.

EDIT:ninja’d

Thanks. Yes maybe I should learn the basics. But I know them. The Problem is: If I am not using them for a long time, I don´t remember them when I need the basics.
I am checkingg if they are in water because if the player hits the water the script shouldn´t be triggered.

I don’t really know why it’s stopping but, in this case you should try using instead then

Entity:EmitSound, as what may be failing is the way of creating and playing the sound

[editline]1st October 2017[/editline]

But, also seeing that you only want to play the sound for the player that has died, i’d instead recommend

surface.PlaySound to EmitSound, as EmitSound and CreateSound are both based on 3D audio, and you seem to only want to play this to the LocalPlayer