Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make an NPC have a death animation?

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

1 comment

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)

1 answer

✓ Accepted answer

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

2 comments

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!

0

No problem!

This discussion is preserved as it was. New answers, comments, and votes are closed.