Animation and FOV change for a sprint script when holding down the key and standing still

Okay so I made a sprint for my game, and I’m testing it and although the script works fine, the fov and the animation play while I am standing still which is not intended, and the fov and animation are aimed to be played when the player moves

this is the script so far and I dont understand wahts wrong since the game checks for if its processed

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("Sprint")
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)
				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)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

You can add a simple line of code to check if the player is actually moving, so that if the player holds CTRL while standing still the animation will not play and the FOV will not change. You can do this with the “MoveDirection” property of the Humanoid.

When the Humanoid.MoveDirection.Magnitude is > 0 then the player is moving.

Here’s what your script should look like after:

local function beginSprint(input, gameProcessed)

	if not gameProcessed then        

		if humanoid.MoveDirection.Magnitude > 0 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)
					sprintAnimationTrack = animator:LoadAnimation(sprintAnimation)
					sprintAnimationTrack.Looped = true
					sprintAnimationTrack:Play()

				end
			end
		end
	end
end
1 Like

okay noted ,thank you ill know this for the future

2 Likes

so ive done this and it fixed the initial problem with when you start moving it starts the animation and fov, however if you hold ctrl still and stop afterwards the animation and fov still occurs, how would i fix this?

2 Likes

You will need it to check if the player is moving every frame (in the same manner as shown above) and cancel the animation and fov tween when movement is 0.

1 Like