Animation not stopping

I am making a tool that will play an animation while you are holding F, this works, but the animation will keep playing even if you stop holding f

inputs.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if not player.Backpack:FindFirstChild("Wood sword") then
			while inputs:IsKeyDown(Enum.KeyCode.F) do
				wait(.1)
				local anim = script.Parent.Block
				local hum = char.Humanoid
				local track = hum:LoadAnimation(anim)
				track:Play()
				print("animation being played")
				if not inputs:IsKeyDown(Enum.KeyCode.F) then
					track:Stop()
				end
				
			end
		end

			end
1 Like

psa: stop loading your animations inside connections. load it in the outer scope and play and stop it accordingly == less memory and less fucking up

also your problem in this case is that code is pretty damn fast so it checks for the while keydown condition before going inside to check the if not keydown and since u no longer press F it breaks the loop

simple fix just stop the animation after the while loop
better fix use inputended

also another thing ur code does is load a new animation every 0.1 second as you are holding F so bye bye memory

1 Like

Your while loop creates a new track every 0.1 seconds, but doesn’t stop all the tracks it creates. For example, it might create a track, check if F is held down (say it is), wait 0.1 seconds, then create another track, check if F is held down (say it’s not) and stop the second track. However, the first track would still be running.

I would instead:

  • Load the animation track outside the loop
  • Create mutliple connections:
    • One to input began (check if the input is F being pressed down and start the track)
    • One to input ended (check if the input is F being released and end the track)
    • And one to unequipped (stop the track)