How do I make an animation cause damage? Like, I made a Roundhouse kick animation. And I want it to cause damage to the person it touches. I tired, it won't work.
01 | wait( 2 ) |
02 |
03 | local player = game.Players.LocalPlayer |
04 | local Mouse = player:GetMouse() |
05 | local humanoid = player.Character.Humanoid |
06 | local s = humanoid:LoadAnimation(game.StarterPack.RoundHouse.Animation) |
07 |
08 | Mouse.KeyDown:connect( function (key) |
09 | if key:lower() = = "f" and humanoid then |
10 | s:Play() |
11 | game.Players.Character.Humanoid.Health = - 20 |
12 | wait( 5 ) |
13 | end |
14 | end ) |
Let's start out by tabbing your code correctly:
01 | wait( 2 ) --Wait 2 seconds |
02 |
03 | local player = game.Players.LocalPlayer --Get the LocalPlayer |
04 | local Mouse = player:GetMouse() --Get the Mouse |
05 | local humanoid = player.Character.Humanoid --Get the Humanoid |
06 | local s = humanoid:LoadAnimation(game.StarterPack.RoundHouse.Animation) --Load animation into Humanoid to be used as a variable |
07 |
08 | Mouse.KeyDown:connect( function (key) --When they press a key |
09 | if key:lower() = = "f" and humanoid then --If that key is f and the humanoid is not nil |
10 | s:Play() -- Play the roundhouse kick animation |
11 | game.Players.Character.Humanoid.Health = - 20 --Set the LocalPlayer's health to -20 |
12 | wait( 5 ) --Wait 5 seconds |
13 | end |
14 | end ) |
Let's now read all of my annotations, explaining what your script does in list form: Wait 2 seconds Get the LocalPlayer Get the Mouse Get the Humanoid Load animation into Humanoid to be used as a variable
When they press a key If that key is f and the humanoid is not nil Play the roundhouse kick animation Set the LocalPlayer's health to -20 Wait 5 seconds Done
I think you can see the problem from this now. In order to make the Kick do damage, you need to do a connection function for if LocalPlayer hits the other. If he does hit the player, do damage.
1 | -- This code sets the health to -20 |
2 | game.Players.Character.Humanoid.Health = - 20 |
3 |
4 | --This code subtracts 20 from the health |
5 | game.Players.Character.Humanoid.Health = game.Players.Character.Humanoid.Health - 20 |
6 |
7 | -- This code also subtracts 20 from the health, but this way is preferred. |
8 | game.Players.Character.Humanoid:TakeDamage( 20 ) |