Animation Wont Stop

I am trying to stop the Animation after the input ends, but it does not work. Can someone please help me?

this is the only error Im getting

Also, please note that im a beginner, so please forgive me if it was a simple mistake

Here is the script: (Client Sided script)

local PLAYR_character = GAME_LOCAL_PLAYER.character
local PLAYER_Humanoid = GAME_LOCAL_PLAYER.character.Humanoid

local PLAYER_CURRECT_CAM = workspace.CurrentCamera

local GAME_REPLICATED_STORAGE = game:GetService("ReplicatedStorage")
local USER_INPUT_SERVICE = game:GetService("UserInputService")

--MISC_VARIABLES

local LEFT_CONTROL_IS_HELD = false
local PLAYER_INCREASE_SPEED = 17
local CLIENT_SERVER_CONNECT = game.ReplicatedStorage:WaitForChild("ClientServerAccessor")

--INPUT_CODE

local function sprintHasBegun(input, gameProcessed)

	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftControl then

			local PlayerFOV = 70

			local Goals = {FieldOfView = PlayerFOV + 30}
			local CameraZoomTweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
			local zoomTween = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, CameraZoomTweenInfo, Goals)
			zoomTween:Play()
			
			local PLAYER_SPRINTING_ANIMATION = Instance.new("Animation")
			PLAYER_SPRINTING_ANIMATION.AnimationId = 'rbxassetid://7437837668'

			local PLAYER_ANIMATION_TRACK_PLAYER = PLAYER_Humanoid.Animator:LoadAnimation(PLAYER_SPRINTING_ANIMATION)
			PLAYER_ANIMATION_TRACK_PLAYER:Play()
			
			LEFT_CONTROL_IS_HELD = true
			
			game.ReplicatedStorage.ClientServerAccessor:FireServer(GAME_LOCAL_PLAYER)
			
		end
	end
end

--INPUT_ENDED_CODE

local function sprintHasEnded(input, gameProcessed)

	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftControl and LEFT_CONTROL_IS_HELD == true then
			
			PLAYER_CURRECT_CAM.FieldOfView = 80
			
			PLAYER_ANIMATION_TRACK_PLAYER:Stop() -- Highlighted in red
			
			game.ReplicatedStorage.SprintHasEnded:FireServer(GAME_LOCAL_PLAYER)
			
		end
	end
end



USER_INPUT_SERVICE.InputBegan:Connect(sprintHasBegun)
USER_INPUT_SERVICE.InputEnded:Connect(sprintHasEnded)
--

You’re creating the animation within the functions scope, so it can’t be not accessed globally. What I suggest is having a global variable that can be set to the AnimationTrack. Also Humanoid:LoadAnimation is deprecated so use the newer atlernative: Animator:LoadAnimation

local AnimationTrack 
local PLAYER_SPRINTING_ANIMATION = Instance.new("Animation")
PLAYER_SPRINTING_ANIMATION.AnimationId = 'rbxassetid://7437837668'

local function sprintHasBegun(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftControl and Player_Humanoid then
			local Animator = Player_Humanoid:FindFirstChildOfClass("Animator")
			if Animator then
				AnimationTrack = Animator:LoadAnimation(PLAYER_SPRINTING_ANIMATION)
			    AnimationTrack:Play()
			end
        end
	end
end

local function sprintHasEnded(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.LeftControl and LEFT_CONTROL_IS_HELD == true then
			AnimationTrack:Stop() -- Highlighted in red			
		end
	end
end

When stopping an animation, you need to stop the animation track, not the animation itself.

The way to do this is to either get the animation from the humanoid, which would be a complicated process, or you could just store the animation track in a global value, as seen in the reply above mine :slight_smile:

Thank you for the solution :slight_smile: , it works now