Animations keeps looping after the character has stopped

  1. What do you want to achieve? Keep it simple and clear!

– Animation stopping whenever the player stops running (or walking)

  1. What is the issue? Include screenshots / videos if possible!
    *btw ignore the bad quality of this because im in a hurry so no OBS rn

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    – Yeah… nothing works really…

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local function sprint(active)
	if isTired then return end
	
	if active then
		hum.WalkSpeed = 24
		for i,v in pairs(hum:GetPlayingAnimationTracks()) do
			v:Stop()
		end
		hum.Animator:LoadAnimation(char:WaitForChild("Animate").run.RunAnim):Play()
	else
		hum.WalkSpeed = 16
		for i,v in pairs(hum:GetPlayingAnimationTracks()) do
			v:Stop()
		end
		hum.Animator:LoadAnimation(char:WaitForChild("Animate").walk.WalkAnim):Play()
	end 
	isRunning = active
end
game:GetService("RunService").Heartbeat:Connect(function(dt)
	if isRunning then
		stamina = math.max(0, stamina - drainRate * dt)
		updateUI()
		print(math.floor(stamina)) -- im still testing out how much stamina is left
		if stamina == 0 then
			sprint(false)
			isTired = false
		end
	else
		stamina = math.min(100, stamina + refreshRate * dt)
		if stamina > staminaRefresh then
			updateUI()
			isTired = false
			print(math.floor(stamina)) -- im still testing out how much stamina is left
			if isHeldingKey then
				sprint(true)
			end
		end
	end
end)

Hope y’all can help me because i’m not the sharpest tool in the shed yet…

1 Like

The problem is that you don’t stop the animations when the player stops moving.

There are a couple ways of checking if the player is moving or not but the most common method is to check the Humanoid’s MoveDirection property. Like so:

if hum.MoveDirection.Magnitude > 0 then
	-- Run walking/running code --
else
	-- Player is not moving, stop animations --
	for i, v in pairs(hum:GetPlayingAnimationTracks()) do
		v:Stop()
	end
	
	-- Recover stamina while the player is standing still here --
	
end

Hope this helps and good luck!

well problem is that i tried it…

and it didnt worked at all…

Can you show me the code you have now?

Well i can, but there’s a problem that is when i gave my partner the place and i deleted mine to save up space, he is currently offline rn so i cant do anything but wait for him to wake up