Issue with stopping a looped animation

Hello DevForum! I am currently having an issue with stopping a looped animation with a script, for some reason it just doesn’t wanna stop, I’ve looked through other forums and couldn’t find an answer, anyone have an idea on how i could stop the animation?

Thanks!

The Script:

local CS = game:GetService("CollectionService")
local DirtCleaned = game.ReplicatedStorage:WaitForChild("Values"):WaitForChild("DirtCleaned")
--local Broom = game.ReplicatedStorage:WaitForChild("Broom"):WaitForChild("Broom")

for i, Dirt in pairs(CS:GetTagged("Dirt")) do
	local Prox = Dirt:WaitForChild("ProximityPrompt")
	local Sound = Instance.new("Sound")
	Sound.SoundId = "rbxassetid://17227776612"
	Sound.Parent = Dirt
	Sound.Volume = 1
	Sound.RollOffMaxDistance = 50
	Sound.RollOffMinDistance = 10
	
	local SweepingAnim = Instance.new("Animation")
	SweepingAnim.AnimationId = "rbxassetid://17295162303"
	
	Prox.PromptButtonHoldBegan:Connect(function(Player)
		local char = Player.Character
		local Humanoid = char:WaitForChild("Humanoid")
		local Animator = Humanoid:WaitForChild("Animator")
		local Broom = char:FindFirstChild("Broom")
		
		if not Broom then
			warn("bro dont got the broom")
		end
		if Broom.Parent == char then
			Sound:Play()
			local SweepingTrack = Animator:LoadAnimation(SweepingAnim)
			SweepingTrack:Play()
		end
	end)
	Prox.PromptButtonHoldEnded:Connect(function(Player)
		local char = Player.Character
		local Humanoid = char:WaitForChild("Humanoid")
		local Animator = Humanoid:WaitForChild("Animator")
		local Broom = char:FindFirstChild("Broom")
		
		if not Broom then
			warn("bro dont got the broom")
		end
		if Broom.Parent == char then
			Sound:Stop()
			local SweepingTrack = Animator:LoadAnimation(SweepingAnim)
			if SweepingTrack.IsPlaying == true then
				SweepingTrack:Stop()
				SweepingTrack.Looped = false
			end
		end
	end)
	
	Prox.Triggered:Connect(function(Player)
		local char = Player.Character
		local Humanoid = char:WaitForChild("Humanoid")
		local Animator = Humanoid:WaitForChild("Animator")
		local Broom = char:FindFirstChild("Broom")
		
		if Sound.IsPlaying then
			Sound:Stop()
		end
		
		if not Broom then
			warn("bro dont got the broom")
		end
		
		if Broom.Parent == char then
			Dirt:Destroy()
			DirtCleaned.Value += 1
			local SweepingTrack = Animator:LoadAnimation(SweepingAnim)
			if SweepingTrack.IsPlaying == true then
				SweepingTrack:Stop()
			end
		end
	end)
end
2 Likes

Keep in mind ever time you use

local SweepingTrack = Animator:LoadAnimation(SweepingAnim)

It rewrites an entirely new animation reference into the variable

local SweepingTrack = Animator:LoadAnimation(SweepingAnim)
SweepingTrack:Play()

local SweepingTrack = Animator:LoadAnimation(SweepingAnim)
SweepingTrack:Stop()

If you played this in studio despite what you’d imigine to happen

SweepingTrack:Stop()

Will not stop the animation because you rewrote the animation reference even if what your trying to load is the same ID.

Another way you can look at it is

local SweepingTrack = Animator:LoadAnimation(SweepingAnim) – animation1
SweepingTrack:Play() – okay play animation1

local SweepingTrack = Animator:LoadAnimation(SweepingAnim) – animation2
SweepingTrack:Stop() – okay stop animation 2 ( no error if you stop a track that hasn’t played )

Id go into more detail on how to help you but currently I’m on mobile but this is a start hope it can help

ah i see, so because i rewrote it 3 different times theyre all practically 3 different objects? Thanks for the help ill try it soon.

1 Like

Yes,

Exactly, especially also when I also saw

local SweepingTrack = Animator:LoadAnimation(SweepingAnim)
if SweepingTrack.IsPlaying == true then
SweepingTrack:Stop()
end

You made a new animation track

local SweepingTrack = Animator:LoadAnimation(SweepingAnim)

Then right after you asked it if the animation was playing however it will always result in False for this if statement

If you or someone else has not already resolved the issue. Pretty sure later in the day when I get in front of my desktop I’ll be able to draft up a potential resolution idea using just your code with that small tweak in it

Thank you! i just made the Animtrack a global variable and it worked.

1 Like