Why isn't this animation script working?

Hello everyone!:wave::slightly_smiling_face:

So I am trying to get a script work that lets a player animation change when their walkspeed is above 18, but the script isn’t working. I also don’t see any errors in the output.

The script:

local id = "10679726519"
local player = game.Players.LocalPlayer
local humanoid = player.Character.Humanoid
local animation = Instance.new("Animation")
animation.Name = "Sprint"
animation.AnimationId = "rbxassetid://" .. id
local animationTrack = humanoid:LoadAnimation(animation)

local id2 = "10862905619"
local animation2 = Instance.new("Animation")
animation2.Name = "Walk"
animation2.AnimationId = "rbxassetid://" .. id2
local animationTrack2 = humanoid:LoadAnimation(animation2)

local function update()
	if player.character.humanoid.WalkSpeed >= 18 then
		animationTrack:Play()
	elseif player.character.humanoid.WalkSpeed <= 18 then
		animationTrack2:Play()
	end
end

How can I fix this?

2 Likes

A bump because I still haven’t got an answer.

Might wanna set the animationtrack’s Priority so that it has the possibility to play.

You should try doing Humanoid.Animator:LoadAnimation() and also check if the Humanoid has a child called Animator before loading the animation. Humanoid:LoadAnimation() is apparently deprecated.

You should also try to set the animation priority to something different, such as movement, however this might not work because of the recent update Roblox did to animation weight.

Also, make sure that the original Roblox animations are not conflicting with this custom one, as that can happen.

Sorry if that didn’t make sense, I’m in a hurry right now.

Don’t use the humanoid when loading animations, it’s deprecated use the animator