Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Player Death sound doesn't work and doesn't display error. Help?

Im trying to make a script so when the player dies it plays a sound. It doesn't work. I think its because im saying where the player is correctly.( i really don't know how to put it) Here's the code

if game.Players.Player.Health == 0 then local sound = game.ServerStorage.Sound sound:Play() end

super short but so im probebly doing something wrong.

1 answer

✓ Accepted answer

There's an event that is triggered when the player is killed. This is called the Died event.

The Died event is a member of the Humanoid, which is a component of the Player's Character.You can setup the died event to trigger your sound.

Now, you have to know where to find the Humanoid of every Character. Sounds tricky but you simply have to setup 2 more events. First will be the PlayerAdded event. Like its name describes, it fires whenever a Player is added to the game. Second will be the CharacterAdded event. It fires whenever a Player's Character is added to the game.

Using these two events, you have access to every potential character in the game. Now you can recieve the Humanoid component and proceed to setup the Died event.

About your sound placement.. If the sound is located in ServerStorage then you won't be able to hear it. Clone it into the Character with the CharacterAdded event, then play the cloned sound with the Died event. The sound will now be produced from the Character itself.

I'll comment the code so you can follow along :)

--Define the original sound
local sound = game.ServerStorage.Sound

--PlayerAdded event. The parameter 'plr' is the newly added player.
game.Players.PlayerAdded:connect(function(plr)
	--CharacterAdded event. 'char' is the character.
	plr.CharacterAdded:connect(function(char)
		--Clone the sound
		soundclone = sound:Clone()
		soundclone.Parent = char:WaitForChild("Torso")
		--Recieve the humanoid
		local hum = char:WaitForChild("Humanoid")
		--Setup the Died event
		hum.Died:connect(function()
			soundclone:Play()
		end)
	end)
end)

Hope I helped!

4 comments

0

Thanks worked now! :D

0

does creating a function like this

0

function test(x) then function below same as what u did above

0

You mean wrap the whole thing in a function and attempt to call it? No.

This discussion is preserved as it was. New answers, comments, and votes are closed.