Roblox animation scripting help

Hey guys im tryna make it so if you click shift you run and run animation plays, im having trouble stopping the animation, heres a clip

https://gyazo.com/a1b43066b2610068c397a5f139660786

local runservice = game:GetService('RunService')
local userinputservice = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild('Humanoid')
local animator = humanoid:WaitForChild('Animator')

local running = false

local walkanimation = Instance.new("Animation")
local runanimation = Instance.new("Animation")
runanimation.AnimationId = 'rbxassetid://8222614475'
walkanimation.AnimationId ='rbxassetid://8222485672'



function getTool()	
	for _, kid in ipairs(script.Parent:GetChildren()) do
		if kid.className == "Tool" then return kid end
	end
	return nil
end




humanoid.Running:Connect(function()
	if humanoid.MoveDirection ~= Vector3.new(0,0,0) then
		if running == false then
			local runtrack = humanoid:LoadAnimation(runanimation)
			runtrack.Priority = Enum.AnimationPriority.Action
			if humanoid.WalkSpeed >= 30 then
				
				runtrack:Play()
				running = true
			else
				
				running = false
				runtrack:Stop()
			end

		end
	else
		print('idle')
		running = false
	end
end)


userinputservice.InputBegan:Connect(function(key, ret)
	if ret then
		return
	else
		if key.KeyCode == Enum.KeyCode.LeftShift then
			humanoid.WalkSpeed = 30
			
			
			
			
			for i = 1,5 do
				game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
				wait()
			end
		end
	end
end)

userinputservice.InputEnded:Connect(function(key, ret)
	if ret then
		return
	else
		if key.KeyCode == Enum.KeyCode.LeftShift then
			humanoid.WalkSpeed = 16
			for i = 1,5 do
				game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
				wait()
			end
		end
	end

end)

code, this is in a local script in startercharacterscripts.

what did you set the animation priority to?

i think debounce script might help?

srry i shouldnt be answering this im not a coder lol

You are re-creating the animiation track every time the event is fired, so the one you are trying to stop is not the same one you played. Move the two lines that create and setup the runtrack outside the connect function so the track is only created and loaded once.

Also the Humanoid:LoadAnimation method is deprecated you should be using the Animator of the Humanoid instead see exampl at Animator | Roblox Creator Documentation

code example on animator if you could :pray:

if not Humanoid:FindFirstChildOfClass("Animator") then
   local Animator = Instance.new("Animator")
   Animator.Parent = Humanoid
end
Humanoid.Animator:LoadAnimation() -- treat this the same way as humanoid:LoadAnimation()

You can delete all of the humanoid.Running function and just use this part

			local runtrack = humanoid:LoadAnimation(runanimation)
			runtrack.Priority = Enum.AnimationPriority.Action
userinputservice.InputBegan:Connect(function(key, ret)
	if ret then
		return
	end
		if key.KeyCode == Enum.KeyCode.LeftShift then
			humanoid.WalkSpeed = 30
			runtrack:Play()
			
			
			
			for i = 1,5 do
				game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
				wait()
			end
		end
	
end)

userinputservice.InputEnded:Connect(function(key, ret)
	if ret then
		return
	end
		if key.KeyCode == Enum.KeyCode.LeftShift then
			humanoid.WalkSpeed = 16
runtrack:Stop()
			for i = 1,5 do
				game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
				wait()
			end
		end
	

end)

Also you should use a tweenservice for the camera fov not a for loop

The link I provided has a code example at the bottom.

local rs = game:GetService('RunService')
local uis = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild('Humanoid')
local camera = workspace.CurrentCamera
local kids = script.Parent

local running = false

local walkanimation = Instance.new("Animation")
walkanimation.Parent = humanoid
walkanimation.AnimationId ='rbxassetid://8222485672'
local walkanim = humanoid:LoadAnimation(walkanimation)
walkanim.Priority = Enum.AnimationPriority.Action

local runanimation = Instance.new("Animation")
runanimation.Parent = humanoid
runanimation.AnimationId = 'rbxassetid://8222614475'
local runanim = humanoid:LoadAnimation(runanimation)
runanim.Priority = Enum.AnimationPriority.Action

local function getTool()	
	for _, kid in ipairs(kids:GetChildren()) do
		if kid:IsA("Tool") then
			return kid
		end
	end
	return nil
end

humanoid.Running:Connect(function()
	if humanoid.MoveDirection ~= Vector3.new(0,0,0) then
		if not running then
			if humanoid.WalkSpeed >= 30 then
				walkanim:Stop()
				runanim:Play()
				running = true
			else
				runanim:Stop()
				walkanim:Play()
				running = false
			end
		end
	else
		running = false
	end
end)

uis.InputBegan:Connect(function(key, proc)
	if proc then
		return
	end

	if key.KeyCode == Enum.KeyCode.LeftShift then
		humanoid.WalkSpeed = 30
		for i = 1, 5 do
			camera.FieldOfView = (70+(i*2))
			task.wait()
		end
	end
end)

uis.InputEnded:Connect(function(key, proc)
	if proc then
		return
	end
	
	if key.KeyCode == Enum.KeyCode.LeftShift then
		humanoid.WalkSpeed = 16
		for i = 1, 5 do
			camera.FieldOfView = (80-(i*2))
			task.wait()
		end
	end
end)