One animation works, the other doesn't

  1. What do you want to achieve? Keep it simple and clear!

I am using an NPC I found in the Toolbox which has its own “stab” animation that is part of the script and is used when the NPC attacks players. I created a “slump” animation in Animation editor that I would like to load in the NPC when a player punches the NPC.

  1. What is the issue?

When a player punches the NPC the slump animation doesn’t work. Nothing happens.

  1. What solutions have you tried so far?

I created a slump animation in Animation editor, added the Animation to the NPC model, and entered the animation Id (http://www.roblox.com/Asset?ID=8381045470) in the properties tab. Then I defined the new animation in the NPC script:

--Existing Animation already in model / script
local stabAnimation = myHuman:LoadAnimation(script.Parent.Stab)
stabAnimation.Priority = Enum.AnimationPriority.Action

--New Animation I created and added by me
local slumpAnimation = myHuman:LoadAnimation(script.Parent.Slump)
slumpAnimation.Priority = Enum.AnimationPriority.Action

Then I added a Bindable Event function in to the script to load the animation when the NPC is punched

local NPCPunched = game.ReplicatedStorage:WaitForChild("NPCPunchedEvent")

NPCPunched.Event:Connect(function(victim, humanoid) 	
	yieldWeapons()	
	slumpAnimation.Priority = Enum.AnimationPriority.Action	
	slumpAnimation:Play()	
end)

I’ve done some experimenting to zero in on the problem:

  1. Since the Slump Animation wasn’t working, I was curious if it was an Animation issue versus a coding issue. So I simply replaced the Slump AnimationId in the properties tab of the Animation with the stab Animation Id and when the NPC is punched it makes a stabbing motion. This tells me my code is correct and must be something to do with the slump animation itself.

  2. When a player punches another player (instead of an NPC), the slump animation works. This tells me the animation and animation Id works.

I cannot identify what the problem is. Any help would be greatly appreciated!

Thank you!

Is the animationID published to Roblox by the same person who owns the game? This is the main reason these things fail.

1 Like

Yes. I own the game and also I published the “slump” animation.

The link above says the page no longer exists. Maybe it was deleted?

What I shared above wasn’t meant to be a link - it’s what I entered into the AnimationId portion of properties and believe it’s the correct formatting (and the same as the stab animation that works).

The actual link to the webpage is: double over - Roblox

Perhaps it might be a loading issue? Try preloading the animations using ContentProvider:PreloadAsync().

I tried your suggestion:

local ContentProvider = game:GetService("ContentProvider")
ContentProvider:PreloadAsync(slumpAnimation)
slumpAnimation:Play()

But I got an error message: “Unable to cast to array”

1 Like

I looked up PreloadAsync and corrected my syntax:

ContentProvider:PreloadAsync({slumpAnimation})

But still the animation doesn’t play

Have you confirmed the event triggers? Some sort of print within the event to know the Play() command has been acknowledged?

Oh, I just noticed you might be using the depreciated Humanoid:LoadAnimation. You need to use Animator:LoadAnimation. Might help!

Humanoid:LoadAnimation (roblox.com)

Here is my latest code based on a bit more research on PreloadAsync:

slumpAnimation.Priority = Enum.AnimationPriority.Action	
local ContentProvider = game:GetService("ContentProvider")

ContentProvider:PreloadAsync({slumpAnimation}, function()
	print("animation was loaded")
end)
			
print ("SLUMP!")
slumpAnimation:Play()

When I run this code the “SLUMP” prints out but the “animation was loaded” does not. So that function isn’t running for some reason?

I don’t know a thing about PreloadAsync. I’m going to go check it out!

Did you see my comment about Humanoid:LoadAnimation? I made an edit to my post, so it may have slipped by.

yes I saw your suggestion. Based on a post on this topic I added an Animator to the humanoid of the NPC. Then wrote this code:

local animator = humanoid:FindFirstChildOfClass("Animator")
if animator then
	local slumpAnimation = animator:LoadAnimation(script.Parent.Slump)
	slumpAnimation.Priority = Enum.AnimationPriority.Action	
	slumpAnimation:Play()
	return slumpAnimation
end

Still no animation played :frowning:

Change “http://www.roblox.com/Asset?ID=8381045470” to “http://www.roblox.com/asset?ID=8381045470”, if this still doesn’t work:

Try using AnimationController. Name it “AnimControl”, Insert it into the NPC, and remove the Humanoid.

Use this code:

local AnimControl = humanoid.Parent:FindFirstChild("AnimControl")

local Animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset?ID=8381045470"
local animationTrack = AnimControl:LoadAnimation(animation)

animationTrack:Play()
1 Like

I had to edit the message twice to fix some errors in the code.

thank you for this suggestion. The lowercase “asset” doesn’t help and the other animation that works (“stab”) uses “Asset” also. About the animation controller, I did use it (see code above). Your suggestion is different which is to replace it with humanoid rather than putting it inside humanoid. My concern with this is that I have other scripts that rely on identifying the Humanoid inside the NPCs.

I would not set the animation priority via script. Set it in the animation editor. One less thing to worry about going wrong (and it currently does not work as expected, Priority does not load properly for up to a second). This should not affect your animation, but better to fix now.

You have all the pieces, so this should work. By any chance did you animate on an R15 but the NPC is an R6, or vice versa?

Tremendous suggestion! I created the animation with a R15 rig and you were right the NPC is an R6. thank you! Hoping that will solve the problem.

1 Like

I know you have one working animation using the other format, but when I look at all my Animation objects, the default format set by Roblox looks like this:

rbxassetid://8381045470

Something else to try? Also, this is how I set up my game services:

Oh good! I was running out of suggestions!

It will, as usually when you animate with a R15 rig, R6 rig’s won’t work with it.