[SOLVED] Animation Playing On Damage Taking

Okay, so, I’m trying to look for a script to help me with my game, where when the Humanoid takes damage, a little animation plays, like a flinching animation, kind of like in The Streets. If there is anyone who could give me a script for that, I would appreciate it, I can handle the animations, as I just need the script.

16 Likes

This doesn’t sound like you’re asking for help, but rather just for someone to give you a script.

2 Likes

I mean, I am asking for help, I have tried scripting it for myself, but the animation didn’t play when I gave it a try.

1 Like

Which of course, the script is gone because I don’t save work that doesn’t “work”.

I would suggest saving work that doesn’t work because you have sites like this and Scripting Helpers.

Scripting Helpers doesn’t help.

They help me a lot, but that’s also why you have here.

1 Like

Essentially, here’s how you would do this:

so physical objects have a Changed property that fires when the object is removed or one of its properties is changed. So the humanoid will fire a Changed event when health is taken away from the Humanoid.Health property. So all we have to do is play the animation once the humanoid.Changed is fired. Here’s an example:

local humanoid = (Humanoid Object Here)
local youranim = humanoid:LoadAnimation(Your Animation Here)

humanoid.Changed:connect(function(property) -- "property" is property that was changed, for now we don't know what it is
if property == "Health" then -- making sure that the changed property is indeed Health
youranim:Play()
end

Edit: this may also fire when health is added, so if you would like to optomize it then you could put a simple check to see if the current health is lower than the previous health.

3 Likes

Okay, but when you load the animation, what should the animation’s PARENT be, the character, or the Humanoid?

For organization purposes, It’s good to parent it to the character or the script, but technically you could parent it to anything, or even nothing!

heres what I mean by nothing:

local anim = Instance.new("Animation") -- notice how i'm not parenting it to anything
anim.Id = "your animation id here"

and then go on from there.

Just make sure to destroy it once you are done using it to avoid memory leaks if you arent parenting it or are continually creating a new animation object

Well, I have tested the script.


It didn’t seem to work when I put in a brick that damages the Humanoid.

1 Like

It just doesn’t work PERIOD, is what I’m saying.

While there are one or two improvements possible, the script looks like it should work. Anything in the output? Do you own the animation? Is the animation priority set high enough? Try putting a print before playing the track do figure out if it is an issue with the animation or your script.

This is what I got in the Output. image

You only wrote the end for the if statement. Append end) to close the function. If you indented your code, it would have been obvious.

2 Likes

Let me try it.

This’s what I got.

Put Damage inside the LocalScript and index it instead by script.Damage. Also, checking whenever Health changes is going to be deceptive. Characters, by default, will auto heal, so it’ll spam the event.

You should instead use HealthChanged and store the old health in a variable. Check if the new health is less than that, and if so play the animation.

No, because I’m not just going to write your script for you.

3 Likes

Well, I can’t do anything more, no matter what I do, the script isn’t working. The animation’s priority is fine and all that, but it just keeps giving me errors in the Output.