Why won't this animation stop?

Hi, I’m a relatively new scripter and I’ve made this bit of code after looking through some tutorials and experimenting. I’m pretty happy with my code except for one formality. Whenever the character only presses the W key once the animation plays regardless. I’ve been trying to only get it to play when I press W + W (which it does). But as mentioned above it also plays when only W is pressed. Here is my code, help would be appreciated.


local UIS = game:GetService("UserInputService")
local DefaultFOV = 70

local lastTime = tick()

local char = game:GetService("Players").LocalPlayer.Character
local hum = char:WaitForChild("Humanoid")

UIS.InputBegan:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.W then
		local now = tick()
		local difference = (now - lastTime)
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://10343328946'
		PlayAnim = char.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
		if difference <= 0.5 then
			if hum.WalkSpeed < 16 then
				print("running")
				hum.WalkSpeed = 32

				local properties = {FieldOfView = DefaultFOV + 20}
				local Info = TweenInfo.new(0.5,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0.5)
				local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera,Info,properties)
				T:Play()
			end
		end
		lastTime = tick()
	end
end)

UIS.InputEnded:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.W then
		PlayAnim:Stop()
		
		print("running")
		hum.WalkSpeed = 15

		local properties = {FieldOfView = DefaultFOV}
		local Info = TweenInfo.new(0.5,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0.5)
		local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera,Info,properties)
		T:Play()

	end

end)

hum.Died:Connect(function()
	game.Workspace.Camera.FieldOfView = DefaultFOV
	hum.WalkSpeed = 10
	script.Disabled = true
end)

Update: I tried to start a custom walking animation whenever the running animation stops but that just ends up breaking the whole thing. Some help is really needed :slight_smile:

I think it’s because the function (including the lasttime/now calculation) is firing each time you hit W, not calculating the difference between when you hit it once, then twice.
You may need an outside variable to check it.

Any idea how I would go about doing this? Sorry I’m pretty new to this scripting stuff.

Try using the Search tool with “double tap input”. I found a few posts about it.

If you’d be so kind to link those posts it would be awesome, also I edited the code a bit and now it’s doing what I want it to do but only after I tap w + w once. The first time I tap the w key two times it doesn’t change the speed and only plays the animation but after that it works great.

local UIS = game:GetService("UserInputService")
local DefaultFOV = 70

local lastTime = tick()

local char = game:GetService("Players").LocalPlayer.Character
local hum = char:WaitForChild("Humanoid")

UIS.InputBegan:Connect(function(input,gameprocessed)
   if input.KeyCode == Enum.KeyCode.W then
   	local now = tick()
   	local difference = (now - lastTime)
   	if difference <= 0.5 then
   		local Anim = game.StarterPlayer.StarterCharacterScripts.Animation
   		PlayAnim = char.Humanoid:LoadAnimation(Anim)
   		PlayAnim:Play()
   		if hum.WalkSpeed < 16 then
   			print("running")
   			hum.WalkSpeed = 32

   			local properties = {FieldOfView = DefaultFOV + 20}
   			local Info = TweenInfo.new(0.5,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0.5)
   			local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera,Info,properties)
   			T:Play()
   		end
   	end
   	lastTime = tick()
   end
end)

UIS.InputEnded:Connect(function(input,gameprocessed)
   if input.KeyCode == Enum.KeyCode.W then
   	
   	PlayAnim:Stop()

   	print("running")
   	hum.WalkSpeed = 15

   	local properties = {FieldOfView = DefaultFOV}
   	local Info = TweenInfo.new(0.5,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0.5)
   	local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera,Info,properties)
   	T:Play()

   end

end)

hum.Died:Connect(function()
   game.Workspace.Camera.FieldOfView = DefaultFOV
   hum.WalkSpeed = 10
   script.Disabled = true
end)

here is the error it’s giving me

I searched “double tap input” and one of the first hits was:
<Double tapping 'WASD' to sprint
Also:
<Making minecraft sprint
<How do i make something if you doubletap a key
<The zoom-zoom: a tutorial-resource for sprinting, dashing, and double-jumps

Please try doing your own research first.

Yeah I did take a look at those but I mentioned before I’m new to this stuff so none of it really made any sense to me. Regardless I did get the script to work, just had to remove an if statement. Thanks for your time.

(I’ll mark you as the solution since you’re the only person who took time to reply)