How do i stop an animation from looping

yo so im adding animations to a sprint in my test game and although the animation works at the start of the sprint, he keeps going after i let go of the designated key and it looks really goofy and bad, ive tried setting the loop to false and the stop animation thing but it hasnt helped and actually made my char have a bit of a seizure

the code

local userInput = game:GetService("UserInputService")
local players = game:GetService("Players")

local player = players.LocalPlayer

local sprintFov = 80
local normalFov = 70
local sprintKey = Enum.KeyCode.LeftControl

local userInputService = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")

local sprintAnimation = script:WaitForChild("ShindenSprint")
wait(1)
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local animator = humanoid:FindFirstChildOfClass("Animator")
local sprintAnimationTrack = animator:LoadAnimation(sprintAnimation)


local defaultSpeed = humanoid.WalkSpeed
local sprintSpeed = (humanoid.WalkSpeed*2)
local camera = workspace.CurrentCamera
local isSprinting = false
local function tweenFov(duration, fov)
	local tween = tweenService:Create(camera, TweenInfo.new(duration, Enum.EasingStyle.Quad), {FieldOfView = fov})
	tween:Play()
	spawn(function()
		tween.Completed:Wait()
		tween:Destroy()
	end)
end

local function beginSprint(input, gameProcessed)

	if not gameProcessed then        

		if input.UserInputType == Enum.UserInputType.Keyboard then

			local keycode = input.KeyCode

			if keycode == Enum.KeyCode.LeftControl then 

				player.Character.Humanoid.WalkSpeed = sprintSpeed
				
				tweenFov(0.2, sprintFov)
				local sprintAnimationTrack = animator:LoadAnimation(sprintAnimation)
				sprintAnimationTrack.Looped = true
				sprintAnimationTrack:Play()
				

			end

		end

	end

end



local function endSprint(input, gameProcessed)

	if not gameProcessed then

		if input.UserInputType == Enum.UserInputType.Keyboard then

			local keycode = input.KeyCode

			if keycode == Enum.KeyCode.LeftControl then

				player.Character.Humanoid.WalkSpeed = defaultSpeed
				sprintAnimationTrack.Looped = false
				sprintAnimationTrack:Stop()
				tweenFov(0.2, normalFov)

			end

		end

	end

end



userInput.InputBegan:Connect(beginSprint)

userInput.InputEnded:Connect(endSprint)

In this line, when the user starts running, you are creating a different animation variable which you cant stop later on. You should remove the local keyword in that line

oh my god im so dumb, thank you :sob:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.