Why is this animation script not working?

Im trying to make it so that when the player jumps, when they land the running animation stops playing.

This has been happening a lot and Im trying to fix it. Its not working and I have no idea why, comments in the code explain parts of the code.

-- Variables --
local playerCharacter = script.Parent

local Humanoid = playerCharacter:WaitForChild("Humanoid")
local WalkAnim = script:WaitForChild("walk")

local state = Humanoid.GetState -- Gets the state of the character ( Running, Walking, Jumping )

local walkingaTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(WalkAnim) -- Track for walking anim
-- Main --

Humanoid.Running:Connect(function(speed)
	if speed > 0 then
		if not walkingaTrack:Play() then -- if the speed is over 0 but walk animation isn't playing then start it (I think??)
			walkingaTrack:Play()
		end
	else
		if state == "Landing" and walkingaTrack.IsPlaying then -- If the Character landed after jumping, and the walking track is playing
			walkingaTrack:Stop() -- Stop the walking animation.
		end
	end
end)
1 Like

this right here :

local walkingaTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(WalkAnim)

is wrong it should be :

local walkingaTrack = Humanoid:LoadAnimation(WalkAnim)
1 Like

@MineGameIdle is incorrect, Humanoid:LoadAnimation() is deprecated and is now recommanded to use Humanoid.Animator:LoadAnimation()

source: Deprecating LoadAnimation on Humanoid and AnimationController

As for the issue the problem is the if statement
if not walkingaTrack:Play() then
Use this instead
if not walkingaTrack.IsPlaying then

Thirdly you need to update your state variable, because its outside of the function it will save the value of whatever state it is during that call and never change and also its a function so you need to do Humanoid:GetState() (make sure to put it inside of the function)

Also your comparsion of the state is incorrect, GetState() wil return a Enum so you need to do something like

if Humanoid:GetState() == Enum.HumanoidStateType.Landed then

1 Like

oh thanks lol i just didnt know that it was since it worked i didnt really know

Would this be good?

-- Variables --
local playerCharacter = script.Parent

local Humanoid = playerCharacter:WaitForChild("Humanoid")
local WalkAnim = script:WaitForChild("walk")
local IdleAnim = script:WaitForChild("Idle")

local state = Humanoid.GetState -- Gets the state of the character ( Running, Walking, Jumping )

local walkingaTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(WalkAnim) -- Track for walking anim
local idleaTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(IdleAnim)
-- Main --

Humanoid.Running:Connect(function(speed)
	if speed > 0 then
		if not walkingaTrack.IsPlaying then -- if the speed is over 0 but walk animation isn't playing then start it (I think??)
			walkingaTrack:Play()
		end
	else
		if state == Humanoid:GetState() == Enum.HumanoidStateType.Landed and walkingaTrack.IsPlaying then -- If the Character landed after jumping, and the walking track is playing
			walkingaTrack:Stop() -- Stop the walking animation.
		end
	end
end)

if Humanoid.WalkSpeed == 0 and walkingaTrack.IsPlaying then
	walkingaTrack:Stop()
	idleaTrack:Play()
end
1 Like

if state == Humanoid:GetState() == Enum.HumanoidStateType.Landed
this is wrong

get rid of the state ==
part

Also you might want to stop the animation in a StateChanged event rather then in the running event

Why so?


Well right now the animation will only stop if the speed is 0 and right when the character lands, might be better to instead always stop the walking animation when the character lands that way u can play some sort of landing animation similar to dark souls, if you don’t need this functionlity doe then its prob fine, although be sure to check if the speed isnt like 0.0001 or something since that can happen sometimes

1 Like