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

How to make an NPC have a death animation?

Asked by 2 years ago

Please bear in mind that I'm a beginner scripter. I started to learn how to script a few days ago.

I have a script that tries to make an NPC to have a death animation when its humanoid health reaches the value 0. However, it doesn't work and I tried everything I know to fix it.

local hum = script.Parent:WaitForChild("Humanoid")
if hum then
    print("Success")
else
    print("No Humanoid")
end
local humanim = hum:LoadAnimation(script:FindFirstChildOfClass("Animation"))
local deathanimation = hum:LoadAnimation(script:FindFirstChildOfClass("Animation"))

if humanim then
    humanim:Play()
    humanim.Looped = true
end 

local function deathanim()
    local death = script.Parent.Script.Died --Died is an animation
    humanim.Looped = false
    humanim:Stop()
    death:Play()
    print("Death animation played!")
    end
if hum.Health == 0 then
    deathanim()
    end
0
Oh and by the way, the script your using Is outdated, Its due to Humanoid:LoadAnimation() being Deprecated (Meaning It doesn't work on new work (Example: Using a Deprecated script on a roblox avater to make It jump,move,etc) imnotaguest1121 362 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

Hello! I think I found a problem, the problem Is that you didn't had a Animator

Here Is the Instructions: 1. Add a NPC 2. Add a Animator Inside of the humanoid of NPC 3. Add a animation Inside of Animator 4. Add script 5. Type this:

local NPC = script.Parent ---The NPC
local Animator = NPC.Humanoid.Animator ----Gets the animator
local Animation = Animator.Animation


while wait() do ----does a loop
    if NPC.Humanoid.Health == 0 then ---If the NPC has 0 health
        local AnimationTrack = NPC.Humanoid.Animator:LoadAnimation(Animation) ---We make a local varible that loads the animation
        AnimationTrack:Play() ---Plays the track (THE ONE BELOW ME IS OPTIONAL)
        wait(1) ----Wait 1 second
        NPC:Destroy() ---Despawns NPC
    end
end

Btw I anchored the HumanoidRootPart or else the body parts will fall off

1
Thanks a lot, you really helped me! You're awesome. I've also managed to make another script that plays when the NPC has above 0 health, and then the animation transitions to death animation with your script. Ty! boijuztgotrecked4 7 — 2y
0
No problem! imnotaguest1121 362 — 2y
Ad

Answer this question