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.
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!