Looped Animation Not Stopping Immediately

I did everything possible to stop the looped animation, I even looped through the humanoid and stop all the animation but it still continues to play when it is supposed to be stopped.

This is what I want but I need to hold the button for a while before unholding it so the looped animation gets stopped:

This is what I don’t want as when I instantly press and not hold the looped animation couldn’t be stopped:

This is the local script:

uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()--change it to player.Character if gui giver
local uis = game:GetService("UserInputService")
local Debounce = true

if not script:FindFirstChild("HoldBlueStartup")then
local anim = Instance.new("Animation",script)
	anim.AnimationId = "http://www.roblox.com/asset/?id=15032508572"
	anim.Name = "HoldBlueStartup" 
	end

if not script:FindFirstChild("HoldBlueDuration")then
local anim2 = Instance.new("Animation",script)
	anim2.AnimationId = "http://www.roblox.com/asset/?id=15032511842"
anim2.Name = "HoldBlueDuration"
end

if not script:FindFirstChild("HoldThrowBlue")then
local anim3 = Instance.new("Animation",script)
	anim3.AnimationId = "http://www.roblox.com/asset/?id=15032525798"
anim3.Name = "HoldThrowBlue"
end

local animload = player.Character.Humanoid:LoadAnimation(script:WaitForChild("HoldBlueStartup"))
local animload2 = player.Character.Humanoid:LoadAnimation(script:WaitForChild("HoldBlueDuration"))
local animload3 = player.Character.Humanoid:LoadAnimation(script:WaitForChild("HoldThrowBlue"))
--New idea for blue, hold blue and charge and when unhold blue it shoots
--can use the reverse curse local script code a reference
--but issue is i also want blue to have a second type, jump in air then it changes 
--to aoe spin 360 blue around the player

uis.InputBegan:Connect(function(key,IsTyping)
	if key.KeyCode == Enum.KeyCode.E then--keybind for the move
		if IsTyping then return end---check if player is typing,if it is then it wont activate.
		if Debounce == true then
			if player.Character.CanAttack.Value == true then--keybind
				if not player.Character:FindFirstChild("Blue1Bool") then
				
				
				if (player.Character.Humanoid:GetState() ~=Enum.HumanoidStateType.Freefall) and (player.Character.Humanoid:GetState() ~=Enum.HumanoidStateType.Jumping) then
					local mana = player.Character.Humanoid.Mana
					local manacost = 14

					if mana.Value > manacost then
						Debounce = false

				--if player.Character:FindFirstChild("DaggerOn")then

						game.ReplicatedStorage.GojoSatoruRemote.GojoRemote1:FireServer("Blue1")
						print("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBblue 1")
						
							
							
							
							
							animload:Play()

							task.wait(0.5)
							
							animload2:Play()
							animload:Stop()
							
							
							
						
					end
					
					end
				end
				
				
				
				
				uis.InputEnded:Connect(function(input)
					if input.KeyCode==Enum.KeyCode.E then
						if player.Character.CanAttack.Value == true then
							if player.Character:FindFirstChild("Blue1Bool") then
								
								animload2:Stop()
								animload3:Play()
								
								local mouse = player:GetMouse()
								game.ReplicatedStorage.GojoSatoruRemote.GojoRemote1:FireServer("Blue2",mouse.Hit.p)
							print("blue 1AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
								
								
								
								
								
								
								end
							end
					end
				end)
				
					
					if player.Character.Humanoid.FloorMaterial == Enum.Material.Air then
						local mana = player.Character.Humanoid.Mana
						local manacost = 14

					if mana.Value > manacost then
						Debounce = false
						
						game.ReplicatedStorage.GojoSatoruRemote.GojoRemote1:FireServer("Blue3")
						print("blue 2")
						end
					end
					
				
				wait(5)
					Debounce = true
				
		end
		
			end
		end
end)






3 Likes

The answer is actually really simple! Instead of continuously trying to stop the animation with a loop, you could use the animation’s Stopped event and a flag variable to introduce a control over when exactly the animation should be allowed to play.

Example:

-- declare a flag to track when the animation is being pressed
local isPressing = false

uis.InputBegan:Connect(function(key, IsTyping)
    if key.KeyCode == Enum.KeyCode.E then
        if IsTyping then return end
          
        if Debounce and player.Character.CanAttack.Value then
            isPressing = true  -- set flag to true as the key is being pressed

            -- start ur animation loop here

        end
    end
end)

uis.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        isPressing = false  -- set flag to false as the key is no longer being pressed
    end
end)

-- connect stopped event to stop looped animation
animload2.Stopped:Connect(function()
    -- Only replay the animation if the key is still being pressed
    if isPressing then
        animload2:Play()
    end
end)

So I think this way looped animation will be playing for as long as key is pressed. When key is released, isPressing is set to false, hence preventing the animation from resuming the next time it stops.

Pleease modify this script according to your need because there could be some variable name mismatch (make sure to replace animload2 and isPressing). When you’re done, reply. I’ll try to assit you further if there still will be smth wrong

2 Likes


The problem is still there, noting changed…

When I instantly press E and unhold E again the holding loop animation still plays even after it is stopped.

animload is the wind up/startup animation.

animload2 is the looped animation that plays as long as the player is holding the button E.

animload3 is the animation that throws the blue orb and before this is played, animload2 is supposed to be stopped.

-- declare a flag to track when the animation is being pressed


local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()--change it to player.Character if gui giver
local uis = game:GetService("UserInputService")
--local Debounce = true
local isPressing = false
if not script:FindFirstChild("HoldBlueStartup")then
	local anim = Instance.new("Animation",script)
	anim.AnimationId = "http://www.roblox.com/asset/?id=15032508572"
	anim.Name = "HoldBlueStartup" 
end

if not script:FindFirstChild("HoldBlueDuration")then
	local anim2 = Instance.new("Animation",script)
	anim2.AnimationId = "http://www.roblox.com/asset/?id=15032511842"
	anim2.Name = "HoldBlueDuration"
end

if not script:FindFirstChild("HoldThrowBlue")then
	local anim3 = Instance.new("Animation",script)
	anim3.AnimationId = "http://www.roblox.com/asset/?id=15032525798"
	anim3.Name = "HoldThrowBlue"
end

local animload = player.Character.Humanoid:LoadAnimation(script:WaitForChild("HoldBlueStartup"))
local animload2 = player.Character.Humanoid:LoadAnimation(script:WaitForChild("HoldBlueDuration"))
local animload3 = player.Character.Humanoid:LoadAnimation(script:WaitForChild("HoldThrowBlue"))

uis.InputBegan:Connect(function(key, IsTyping)
	if key.KeyCode == Enum.KeyCode.E then
		if IsTyping then return end
		
		--if Debounce == true then
			if player.Character.CanAttack.Value == true then--keybind
				if not player.Character:FindFirstChild("Blue1Bool") then 
					isPressing = true
					--Debounce = false  -- set flag to true as the key is being pressed

					-- start ur animation loop here
					game.ReplicatedStorage.GojoSatoruRemote.GojoRemote1:FireServer("Blue1")



					animload:Play()

					wait(0.5)

					animload:Stop()
					animload2:Play()
				end
			
		end
	end
end)

uis.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if player.Character.CanAttack.Value == true then
			if player.Character:FindFirstChild("Blue1Bool") then

				isPressing = false 
				
				animload2:Stop()
				animload3:Play()

				local mouse = player:GetMouse()
				game.ReplicatedStorage.GojoSatoruRemote.GojoRemote1:FireServer("Blue2",mouse.Hit.p)
			end
			end-- set flag to false as the key is no longer being pressed
	end
end)

-- connect stopped event to stop looped animation
animload3.Stopped:Connect(function()
	-- Only replay the animation if the key is still being pressed
	if isPressing == false then
		
		if animload3.IsPlaying then
			animload3:Stop()
		end
		
		
	end
end)

1 Like

Hmmm. The issue may be that animations in Roblox do not stop instantly, like there will be a small delay or they finish their current loop before stopping. So, when the “E” key is released, rather than stopping the animation instantly, it completes the cycle before stopping, so if we use debounce and timer system this could potentially solve this issue.

This is how I’d do it:

  1. Set a debounce when animation starts/when “E” is pressed.

  2. Set a timer for the length of the animation loop.

  3. When “E” key is released:
    a. Stop the animation.
    b. Check if the debounce is still active (if the timer has not run out).
    c. If debounce is active, delay the throw animation until the debounce is stopped.

  4. Once the debounce is stopped (animation loop has ended), play the throw animation.

Here’s a rewrite of your code considering the above:

local isPressing = false
local debounce = false
local animTime = 1 --^ Length of your animation in seconds ^--
local StartAnim = chara:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("HoldBlueStartup"))
local LoopAnim = chara:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("HoldBlueDuration"))
local EndAnim = chara:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("HoldThrowBlue"))

uis.InputBegan:Connect(function(key, IsTyping)
	if key.KeyCode == Enum.KeyCode.E then -- keybind for the move
		if isTyping then return end
		if chara.CanAttack.Value then
			if not debounce then
				isPressing = true
				debounce = true

				-- play start animation
				StartAnim:Play()

				wait(animTime)
				debounce = false

				if isPressing then
					-- play loop animation
					LoopAnim:Play()
				else
					-- play end animation
					EndAnim:Play()
				end
			end
		end
	end
end)

uis.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		if chara.CanAttack.Value then
			if isPressing then
				isPressing = false
				LoopAnim:Stop()

				if not debounce then
					-- play end anim
					EndAnim:Play()
				end
			end
		end
	end
end)

Also dont forget to replace the animTime with the correct length of your animation. I am ready to help you further if, again, something goes wrong

2 Likes

Yup it worked, thanks alot! :smiley: Sorry for the late replies :sweat_smile:

1 Like

That’s great, glad to help you! Good luck on the project :+1:

1 Like

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