Why is my animation still playing after I stopped it?

I have a ContextActionService that fires remotes events, but I also have it play animations. There are 2 animations, and one of them is charging the sword behind the player, and one is slamming the sword down. I make it so the animation is charging, then if the player lets go of the keybind, it slams down. If they hold it for a long time, it will eventually slam down. I’m trying to make it so you can charge the sword then when you let go of the key, it slams down. It works, BUT, after it slams down, it goes to charging. I don’t know why the charging animation is still playing after the slamming animation.

Code:
IGNORE THE DEBOUNCES EXCEPT TWOEND AND TWOHOLD

local function handleAction(actionName, inputState, inputObject)
	print("Disabled: ",disabled)
	if disabled == false then
		local anim1 = Character:FindFirstChild("Humanoid"):LoadAnimation(script.swordup)
		local anim2 = Character:FindFirstChild("Humanoid"):LoadAnimation(script.sworddown)
		if inputState == Enum.UserInputState.Begin and twodebounce == false and mousedebounce == false and onedebounce == false then

			onedebounce = true
			pullbounce = true
			twodebounce = true
				mousedebounce = true
				print(actionName, inputObject)
				game.ReplicatedStorage.Rainbowsword:FireServer("2up")
				print("two pressed")
				
				anim1:Play()
			anim1.Stopped:wait()
			anim1:Stop()
			print("anim1 stopped")
			if twohold == false then
				print("twohold is false")
				twohold = true
				
			game.ReplicatedStorage.Rainbowsword:FireServer("2down")
			anim2:Play()
				anim2.Stopped:wait()
				anim2:Stop()
				game.ReplicatedStorage.Rainbowsword:FireServer("2done")
				end
			
		end
		if inputState == Enum.UserInputState.End then
			if twoend == false then
				anim1:Stop()
				twoend = true
				twohold = true
				print("2 stopped holding")
				anim1:Stop()
				print(anim1.IsPlaying)
				game.ReplicatedStorage.Rainbowsword:FireServer("2down")
				anim2:Play()
				print(twoend)
				print(twohold)
				anim2.Stopped:wait()
				print(twoend)
				print(twohold)
				anim1:Stop()
				anim2:Stop()
				
				game.ReplicatedStorage.Rainbowsword:FireServer("2done")
				end
			
		end
		
	end
	
end

ContextActionService:BindAction("2", handleAction, true, Enum.KeyCode.Two)
ContextActionService:SetPosition("2",UDim2.fromScale(0.3,0.43))
ContextActionService:SetTitle("2", "2")

What happens:
https://gyazo.com/7bdc77c11012d385135f21e6516ecb77
As you can see, after I slammed the sword down it goes back to the charging animation. I don’t know if I did something wrong or :Stop() doesn’t work.

Your script is pretty long, but it looks like you recreate the animation each time. The anim1 and anim2 variables should be declared outside your function (variables are declared wherever you add local, unless they are global).

Example

local anim1
local anim2
local function handleAction(actionName, inputState, inputObject)
	print("Disabled: ",disabled)
	if disabled == false then
		anim1 = Character:FindFirstChild("Humanoid"):LoadAnimation(script.swordup)
		anim2 = Character:FindFirstChild("Humanoid"):LoadAnimation(script.sworddown)
		if inputState == Enum.UserInputState.Begin and twodebounce == false and mousedebounce == false and onedebounce == false then

			onedebounce = true
			pullbounce = true
			twodebounce = true
				mousedebounce = true
				print(actionName, inputObject)
				game.ReplicatedStorage.Rainbowsword:FireServer("2up")
				print("two pressed")
				
				anim1:Play()
			anim1.Stopped:wait()
			anim1:Stop()
			print("anim1 stopped")
			if twohold == false then
				print("twohold is false")
				twohold = true
				
			game.ReplicatedStorage.Rainbowsword:FireServer("2down")
			anim2:Play()
				anim2.Stopped:wait()
				anim2:Stop()
				game.ReplicatedStorage.Rainbowsword:FireServer("2done")
				end
			
		end
		if inputState == Enum.UserInputState.End then
			if twoend == false then
				anim1:Stop()
				twoend = true
				twohold = true
				print("2 stopped holding")
				anim1:Stop()
				print(anim1.IsPlaying)
				game.ReplicatedStorage.Rainbowsword:FireServer("2down")
				anim2:Play()
				print(twoend)
				print(twohold)
				anim2.Stopped:wait()
				print(twoend)
				print(twohold)
				anim1:Stop()
				anim2:Stop()
				
				game.ReplicatedStorage.Rainbowsword:FireServer("2done")
				end
			
		end
		
	end
	
end

ContextActionService:BindAction("2", handleAction, true, Enum.KeyCode.Two)
ContextActionService:SetPosition("2",UDim2.fromScale(0.3,0.43))
ContextActionService:SetTitle("2", "2")

There also might be other problems. This is just one I noticed.

Edit:
Glad I could help :smile:

OMG IT WORKED THXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX. thanks

1 Like