Pause animation when holding the "E" key

Hello! I’ve been trying to pause an animation when a player is holding the “E” key. The code works properly on the first time, but then it started to get some errors.
The problem is, when I’m only pressing it once, it will still pause the animation. How do I make it that the animation will only stop whenever im holding a key, and it will play again whenever I release the key?

Here’s the code :

– local script

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")

local fireEvent = RS:WaitForChild("Fire")
local endEvent = RS:WaitForChild("End")

	UIS.InputBegan:Connect(function(input, typing)
	if input.KeyCode == Enum.KeyCode.E and typing == false then
		fireEvent:FireServer()
	end
end)

UIS.InputChanged:Connect(function(input, GPE)
	UIS.InputEnded:Connect(function(input, typing)
		if input.KeyCode == Enum.KeyCode.E and typing == false then
			endEvent:FireServer()
		end
	end)
end)

– Server script

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		
		local RS = game:GetService("ReplicatedStorage")
		local fireEvent2 = RS:WaitForChild("Fire")
		local endEvent2 = RS:WaitForChild("End")

		local db = false

		fireEvent2.OnServerEvent:Connect(function(player)
			if db == false then
				db = true
				local animation = Instance.new("Animation")
				animation.AnimationId = "rbxassetid://9168220359"
				local hikenPart = script.HikenFlame:Clone()
				local animationTrack = player.Character.Humanoid:LoadAnimation(animation)
				animationTrack:Play()
				character.Humanoid.WalkSpeed = 0
				character.Humanoid.JumpHeight = 0
				character.Humanoid.AutoRotate = false
				animationTrack:GetMarkerReachedSignal("Charge"):Connect(function(paramString)
					local fire = script.HandFlame.Attachment:Clone()
					fire.Parent = player.Character.RightHand
					animationTrack:AdjustSpeed(0)
					endEvent2.OnServerEvent:Connect(function(player)
						animationTrack:AdjustSpeed(1)
						fire:Destroy()
					end)
					animationTrack:GetMarkerReachedSignal("fireHiken"):Connect(function(paramString)
						local startPos = character.RightHand.Position
						local endPos = character.HumanoidRootPart.Position + character.HumanoidRootPart.CFrame.LookVector * 100
						local antiGravity = .5
						local bf = Instance.new("BodyForce")
						bf.Force = Vector3.new(0, hikenPart:GetMass() * 196, 0)
						bf.Parent = hikenPart
						hikenPart.CFrame = CFrame.new(startPos, endPos)
						hikenPart.Parent = workspace
						hikenPart.Velocity = hikenPart.CFrame.LookVector * 70
						hikenPart.Touched:Connect(function(hit)
							if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= character.Name then
								local humanoid = hit.Parent.Humanoid
								humanoid.Health -= 10
							end
						end)
						animationTrack.Stopped:Wait()
						hikenPart:Destroy()
						character.Humanoid.WalkSpeed = 16
						character.Humanoid.JumpHeight = 7.5
						character.Humanoid.AutoRotate = true
						db = false	
					end)
				end)
			end
		end)
	end)
end)

Any Help is appreciated ! Thanks !

why are you using UIS.InputChanged? if you want to constantly check if the input ended you can just Use RunService.Heartbeat and then put the UIS.InputEnded code in there.

1 Like

Alright thanks, I will try it! I know that it’s not how I use user input changed but at least I wanna try it, maybe something will happen unexpectedly!

1 Like
local userInput = game:GetService"UserInputService"
local players = game:GetService"Players"
local player = players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild"Humanoid"
local animator = humanoid:WaitForChild"Animator"

local debounce = false

local function onInputBegan(inputObject, wasProcessed)
	if debounce then return end
	if wasProcessed then return end
	
	if inputObject.KeyCode == Enum.KeyCode.E then
		debounce = true
		local humanoidTracks = humanoid:GetPlayingAnimationTracks()
		local animatorTracks = humanoid:GetPlayingAnimationTracks()
		local animationTracks = table.move(animatorTracks, 1, #animatorTracks, #humanoidTracks + 1, humanoidTracks)
		for _, animationTrack in ipairs(animationTracks) do
			animationTrack:Stop()
		end
		task.wait(0.5)
		debounce = false
	end
end

userInput.InputBegan:Connect(onInputBegan)

This will stop all tracks being played through the character’s humanoid/animator instance upon the “E” key being pressed.

1 Like

Thanks but It didn’t work, I still got the same result as before

1 Like

Thanks but what if I want to pause the animation when I’m holding the “E” key but not pressed the “E” key?