Why won't my animation stop playing?

I got help to make a toggle sprint system, and I want a sprint animation to play when the character moves. It works but after the animation starts it won’t stop.

local character = script.Parent 
local Anim = Instance.new('Animation')
local humanoid = character:WaitForChild("Humanoid")
local IsRunning = false

UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftControl then
IsRunning = not IsRunning
if IsRunning == true then
			Character.Humanoid.WalkSpeed = 35
			humanoid.Running:Connect(function(speed)
				if speed >= 30  then 
	Anim.AnimationId = 'rbxassetid://5362333091'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
					  PlayAnim:Play()
					if speed <= 1 then
						 PlayAnim:Stop()
					end
				end
end)

else
			Character.Humanoid.WalkSpeed = 15
			  PlayAnim:Stop()
		end
	end
end)

This is my local script, can I get some help?

If the character’s speed is originally 15 why set the speed to check if it is less than 1? The script wouldn’t be able to process what is going on if the original speed is 15 but it is reading for 1.

Wait I see what you’re saying can you send a video of what is going on?

instead of using only InputBegan and doing IsRunning = not IsRunning,

You should instead make use if InputEnded to handle the stopping with the button being unpressed.

Edit: oh you have this act as a toggle. Nevermind lol

It will probably be bad footage because my laptop is too old to screenrecord XD

I found your error. In the >= 30 check, you put the <= 1 inside it which it never reaches because its >= 30.

Here is the fixed code:

local character = script.Parent 
local Anim = Instance.new('Animation')
local humanoid = character:WaitForChild("Humanoid")
local IsRunning = false

local connection = false

UIS.InputBegan:Connect(function(input, gpe)
	if input.KeyCode == Enum.KeyCode.LeftControl and not gpe then
		IsRunning = not IsRunning
		if IsRunning then
			humanoid.WalkSpeed = 35
			connection = humanoid.Running:Connect(function(speed)
				if speed >= 30  then 
					Anim.AnimationId = 'rbxassetid://5362333091'
					PlayAnim = Character.Humanoid:LoadAnimation(Anim)
					PlayAnim:Play()
				elseif speed <= 1 then
					PlayAnim:Stop()
				end
			end)
		else
			humanoid.WalkSpeed = 15
			PlayAnim:Stop()
			if connection then
				connection:Disconnect()
			end
		end
	end
end)
1 Like

Thank you so much, I can finally fix my sleep schedule.

3 Likes

Lol i know how that is when you’re stuck trying to fix one thing. Glad you found your solution :grin:

1 Like

ahem ahem ummmmmmmmmmmmm noticed problem

Once you change direction the animation never stops…

Try adding a wait(0.01) in there or just a wait() so the script can cooldown.

1 Like

Once i change directions after going in a direction it breaks. Wait didn’t help…

That’s weird can you try to record a video the best you can so I can see?

File was too big that was best I could do

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
local character = script.Parent 
local Anim = Instance.new('Animation')
local humanoid = character:WaitForChild("Humanoid")
local IsRunning = false

local connection = false

UIS.InputBegan:Connect(function(input, gpe)
	if input.KeyCode == Enum.KeyCode.LeftControl and not gpe then
		IsRunning = not IsRunning
		if IsRunning then
			humanoid.WalkSpeed = 35
			connection = humanoid.Running:Connect(function(speed)
				if speed >= 30  then 
					Anim.AnimationId = 'rbxassetid://5362333091'
					PlayAnim = Character.Humanoid:LoadAnimation(Anim)
					PlayAnim:Play()
				elseif speed <= 1 then
					PlayAnim:Stop()
					wait(0.1)
					PlayAnim:Stop()
				end
			end)
		else
			humanoid.WalkSpeed = 15
			PlayAnim:Stop()
			if connection then
				connection:Disconnect()
			end
		end
	end
end)

So you want that specific animation to play? Why don’t you just use a roblox character’s animate script and put the animation id in the script.

I want a different walk animation and a different run

Yep you can do all that using a regular player animate script!

It didn’t work for me, changing the walk is fine but the run does nothing

For the run if you’re using the same animation as the walk you need to make 2 of the same one for both of them to work.

I need to make 2 animations?

30characters

i wanted a different walk and sprint anim.

But in the video you see while I’m turning the animation plays multiple times, what does this mean?( you can tell by my leg kicking up)