Animation Not Stopping With Scripts

I got a script, everything works, but… the animation will not stop and i cant figure out why.

–The Script

game.ReplicatedStorage.PlayerFire.CrouchEvent.OnServerEvent:Connect(function (player, hum, value)
	if hum then
		hum.WalkSpeed = value
	end
	
	print(value)
	
	if hum then
		local animator = hum:FindFirstChild("Animator")
		local animTrack = animator:LoadAnimation(game.ReplicatedStorage.Animations.Crouch)
		if hum.WalkSpeed == 10 and not hum.Sit and not animTrack.IsPlaying then
			animTrack:Play()
		elseif hum.WalkSpeed == 16 then
			animTrack:Stop()
		end
	end
end)

I’m going to assume it’s an issue with the first if condition that controls the animation, since all the conditions are combined:

if (hum.WalkSpeed == 10) and (not hum.Sit) and (not animTrack.IsPlaying) then

Try this.

nothing changed, do you have any other suggestions?

1 Like

Huh, try printing something out when animTrack:Play() and animTrack:Stop() runs. Also, just before the if statement, print out hum.WalkSpeed.

game.ReplicatedStorage.PlayerFire.CrouchEvent.OnServerEvent:Connect(function (player, hum, value)
	if not hum or not hum:IsA("Humanoid") then return end

	hum.WalkSpeed = value
	
	print(value)
	

	local animator = hum:FindFirstChild("Animator")
	local animTrack = animator:LoadAnimation(game.ReplicatedStorage.Animations.Crouch)
	if value == 10 and not hum.Sit and not animTrack.IsPlaying then
        print(value, "Is crouching")
		animTrack:Play()
	elseif value == 16 then
        print(value)
		animTrack:Stop()
	end

end)

thats what i’ve got setup but still, it won’t work, everything prints out right but it the animTrack:Stop() just wont work

i looked into it and found the solution, here it is.

local crouchAnim = game.ReplicatedStorage.Animations.Crouch
local animTrack -- Declare a global variable for the animation track

game.ReplicatedStorage.PlayerFire.CrouchEvent.OnServerEvent:Connect(function (player, hum, value)
    hum.WalkSpeed = value
    print(value)

    local animator = hum:FindFirstChild("Animator")
    if not animTrack then
        animTrack = animator:LoadAnimation(crouchAnim)
        animTrack.Priority = Enum.AnimationPriority.Action
        animTrack.Looped = false

        animTrack.Stopped:Connect(function()
            print("Animation has stopped")
        end)
    end
    
    if value == 10 and not hum.Sit and not animTrack.IsPlaying then
        print(value, "Is crouching")
        animTrack:Play()
    elseif value == 16 then
        print(value, "Attempting to stop animation")
        print("IsPlaying:", animTrack.IsPlaying)
        print("Length:", animTrack.Length)
        if animTrack.IsPlaying then
            print("Animation is playing, stopping now")
            animTrack:Stop()
        else
            print("Animation is not playing")
        end
    end
end)