Stopping animations from overlapping or looping

V Code below calls to stop player’s animation. If the player is jumping or moving while this code runs, it will cause what’s happening in the video above.

local humanoid = LocalPlayer.Character.Humanoid
local humanoidStatesTable = {
	"FallingDown",
	"Running",
	"RunningNoPhysics",
	"Climbing",
	"GettingUp",
	"Jumping",
	"Flying",
	"Freefall",
}


-- Stop all animations 
for _, playingTrack in ipairs(humanoid.Animator:GetPlayingAnimationTracks()) do
	playingTrack:AdjustWeight(0)
	playingTrack:Stop()
	playingTrack:Destroy()
	playingTrack = nil
end

-- Turn back on humanoid states
for _, humState in ipairs(humanoidStatesTable) do
	humanoid:SetStateEnabled(Enum.HumanoidStateType[humState], true)
end

I think this might be due to animations being forced to stop while the player moves, jumps, or runs causing the Default Roblox Animations to potentially overlap and loop forever. Any potential fixes for this?

Hi there,

This is happening because your code successfully stops all animations being played at the time it is ran, but it doesn’t consistently check for, or stop new animations from playing.

The below modification so sort out this issue for you with the AnimationPlayed event hooked to a function that stops and destroys new animations as soon as they’re played.

local Player = game:GetService("Players").LocalPlayer
local humanoid = Player.Character.Humanoid
local animator = humanoid:FindFirstChildWhichIsA("Animator")
local connection

local humanoidStatesTable = {
	"FallingDown",
	"Running",
	"RunningNoPhysics",
	"Climbing",
	"GettingUp",
	"Jumping",
	"Flying",
	"Freefall",
}

-- Stop all animations 
if animator then
	for _, playingTrack in ipairs(animator:GetPlayingAnimationTracks()) do
		playingTrack:AdjustWeight(0)
		playingTrack:Stop()
		playingTrack:Destroy()
	end
	
	connection = animator.AnimationPlayed:Connect(function(playingTrack)
		playingTrack:Stop()
		playingTrack:Destroy()
	end)
end

-- Turn back on humanoid states
for _, humState in ipairs(humanoidStatesTable) do
	humanoid:SetStateEnabled(Enum.HumanoidStateType[humState], true)
	
	if connection and connection.Connected then
		connection:Disconnect()
	end
end

Hope this helps. Cheers.

2 Likes

Unfortunately came up with more problems. Though I do still find this to be helpful. Thanks!

1 Like

I’m sorry to hear that. Were the issues surrounding the method not correctly working? Or is it creating other issues surrounding the rest of your codebase and functions? Depending on that, it can potentially even be modified to reduce or counter any conflicts.