✓ 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 :)
local sound = game.ServerStorage.Sound
game.Players.PlayerAdded:connect(function(plr)
plr.CharacterAdded:connect(function(char)
soundclone = sound:Clone()
soundclone.Parent = char:WaitForChild("Torso")
local hum = char:WaitForChild("Humanoid")
hum.Died:connect(function()
soundclone:Play()
end)
end)
end)
Hope I helped!
4 comments
Thanks worked now! :D
does creating a function like this
function test(x) then function below same as what u did above
You mean wrap the whole thing in a function and attempt to call it? No.