Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 8 years ago

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

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago
Edited 8 years ago

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!

0
Thanks worked now! :D NickDestroyer123 12 — 8y
0
does creating a function like this NickDestroyer123 12 — 8y
0
function test(x) then function below same as what u did above NickDestroyer123 12 — 8y
0
You mean wrap the whole thing in a function and attempt to call it? No. Goulstem 8144 — 8y
Ad

Answer this question