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

How to use the OnDied() function?

Asked by 9 years ago

All I need is the connection. That's what know:

function OnDied()

end
--Here goes the connection

2 answers

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

The Died event is an event of Humanoids.

To get the Humanoid, it is usually done with anonymous functions, so we can "stack" our events. Remember that died events only run a single time; after the Humanoid dies once, the character will get a new Humanoid, thus making your previous connection invalid. Using the CharacterAdded event defeats this.

game.Players.PlayerAdded:connect(function(plr)
    plr.CharacterAdded:connect(function(chr)
        chr:WaitForChild("Humanoid").Died:connect(function()
            print(plr.Name.." died!")
        end)
    end)
end)

There are likely ways to do this without anonymous functions, but they will probably be much messier.

0
How do you mean we can "'stack' our events?" Can you please explain that? Redbullusa 1580 — 9y
2
If you look at how his code is structured, there are three events nested inside each other. By using anonymous function, the scripter can more easilly write such a convuluted structure, as it's a lot easier to mentally keep track of where everything is. It also allows for the inner functions to inherit the environment of the outer functions. adark 5487 — 9y
0
^ Perci1 4988 — 9y
0
Thanks a lot it worked! TheBigCoder 25 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Do you Mean this?

game.Players.PlayerAdded:connect(function(plr)
    plr.CharacterAdded:connect(function(chr)
        chr:WaitForChild("Humanoid").Died:connect(function()
            print(plr.Name.." died!")
        end)
    end)
end)
0
what is the 'Charcter' variable? TheBigCoder 25 — 9y
0
I would appreciate if accept my Answer if I helped you with your Problem. UserOnly20Characters 890 — 9y
1
The Character is *not* another name for the Player. The character is the 3-d object running around on your computer screen in game.Workspace, while the player does pretty much everything else. The player is located in game.Players. Perci1 4988 — 9y

Answer this question