Animations sometimes still continuing after being stopped

I have a code that stops a animation at a certain point, the code would work some of the times but it wouldn’t always work. Right now I’m using the GetMarkerReachedSignal event to stop it:

	animation:GetMarkerReachedSignal("Casting"):Connect(function()
		animation:AdjustSpeed(0)
	end)

I have also tried using another method to stop the animation, which was the AnimationStopped event. But that didn’t always work too:

animation.Stopped:Connect(function()
	animation:AdjustSpeed(0)
end)

Edit - Here’s my full script just in case:

local FlamethrowerPart = game.ServerStorage.Magics.Fire.FlamethrowerPart
local FlamethrowerEvent = game.ReplicatedStorage.Magics.Fire:WaitForChild("Flamethrower")
local TweenService = game:GetService("TweenService")
local hitTable = {}
local damaged = {}

local dmg = 2
local burnlength = 4
local speed = 20
local StunDuration = 0.2
local lifetime = 2
local amountofparts = 8
local animid = "rbxassetid://10808173944"

FlamethrowerEvent.OnServerEvent:Connect(function(player)
	local humanoid = player.character:WaitForChild("Humanoid")

	game.ReplicatedStorage.GlobalCd.MoveStart:FireClient(player)

	local castanim = Instance.new("Animation")
	castanim.AnimationId = animid
	local animation = humanoid:LoadAnimation(castanim)
	animation:Play()
	
	local oldws = humanoid.WalkSpeed
	local oldjp = humanoid.JumpPower
	
	humanoid.WalkSpeed = 2
	humanoid.JumpPower = 0
	
	animation:GetMarkerReachedSignal("Casting"):Connect(function()
		animation:AdjustSpeed(0)
	end)
	
	task.wait(0.5)

	local newFlamethrower = FlamethrowerPart:Clone()

	for i = 1,amountofparts,1 do
		local newFlamethrower = FlamethrowerPart:Clone()
		newFlamethrower.Parent = game.Workspace
		newFlamethrower.CFrame = player.character.HumanoidRootPart.CFrame*CFrame.new(Vector3.new(0,0,-1))
		local BodyVelocity = Instance.new("BodyVelocity")
		BodyVelocity.Parent = newFlamethrower
		BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		BodyVelocity.Velocity = player.character.HumanoidRootPart.CFrame.LookVector*speed
		game.Debris:AddItem(newFlamethrower,lifetime)

		local newThread = coroutine.create(function()
			for i = 1,500,1 do
				local x, y, z = math.random(-360,360), math.random(-360,360), math.random(-360,360)

				newFlamethrower.Size = newFlamethrower.Size + Vector3.new(0.1,0.1,0.1)
				newFlamethrower.Orientation = newFlamethrower.Orientation + Vector3.new(x,y,z)
				task.wait()
			end
		end)
		coroutine.resume(newThread)

		newFlamethrower.Touched:Connect(function(hit)
			if hit.Parent.Name ~= player.Name and hit.Parent:FindFirstChild("Humanoid") then
				local ehumanoid = hit.Parent.Humanoid
				
				table.insert(hitTable, ehumanoid)
				
				for i = 1, #hitTable, 1 do
					if table.find(damaged, ehumanoid) then return end
					
					ehumanoid.Health -= dmg
					
					local stuntag = Instance.new("ObjectValue")
					stuntag.Name = "StunEffect"
					stuntag.Parent = ehumanoid
					game.Debris:AddItem(stuntag,StunDuration)

					local burntag = Instance.new("ObjectValue")
					burntag.Name = "BurnEffect"
					burntag.Parent = ehumanoid
					game.Debris:AddItem(burntag,burnlength)

					local Tagged = Instance.new("ObjectValue")
					Tagged.Name = "killer"
					Tagged.Value = player
					Tagged.Parent = ehumanoid
					
					table.insert(damaged, ehumanoid)
				end

				end
			end)
		
		task.wait(0.12)
		hitTable = {}
		damaged = {}
		
	end
	
	humanoid.WalkSpeed = oldws
	humanoid.JumpPower = oldjp
	animation:AdjustSpeed(1)
	game.ReplicatedStorage.GlobalCd.MoveFinish:FireClient(player)

end)

And here’s a video of an instance where the animation stops and an instance where it continues to play after I stopped it. (Btw, I don’t think the walking animation is causing this since the default walking animation causes this too):

1 Like

Try debugging your code first. Does the function even fire? Print something within the function, if its not printing then there is something wrong with the way you’re calling your function. If you’re calling GetMarkerReachedSignal and :AdjustSpeed() on a loaded animation then it should be firing properly (pretty sure it would throw an error otherwise)

Also calling :Stopped on an animation to… well, stop the animation probably wouldn’t actually make a difference, the event firing is already implying that the animation has stopped and doesn’t need further stopping :stuck_out_tongue:

There is nothing inherently wrong with your code, what’s important is that the function is even being called.

If you know the function is being reached and there is no error being thrown, it could be possible that “Casting” isn’t even a marker in your animation. Keep in mind there are two ways of using “markers” in animation, there are keyframe names and markers, they are both called using different methods:

https://developer.roblox.com/en-us/api-reference/event/AnimationTrack/KeyframeReached

and the one you’re using:
https://developer.roblox.com/en-us/api-reference/function/AnimationTrack/GetMarkerReachedSignal

1 Like

Yes my function does work sometimes, it is being called. But it isn’t always working.

1 Like

Sorry made an edit after your reply ^ :sweat_smile:

1 Like

If you want to completely stop an animation you could straight up call the Stop() event, AdjustSpeed() only works on playing animations, meaning they need to be actively running to change the speed (since AdjustSpeed() doesn’t actually stop an animation, it does what the name implies and just adjusts the playback speed of the animation, useful for animations like adaptive run speed animation)

1 Like

Yeah I’m using AdjustSpeed to stop an animation when it reaches a certain point, then to resume it after an event. I just tried using the Stop() event, but for some reason it isn’t really working and I’m not sure how to resume the animation after that. Here’s my code:

	animation:GetMarkerReachedSignal("Casting"):Connect(function()
		animation:Stop()
	end)

Yeah this is definitely how you would want to approach pausing your animation.

There might be some conflicting code where the condition for starting your animation back up might be too closely related to you stopping your animation, or that function might be getting called too early/quickly. I don’t know what the rest of your code looks like so this is purely based on assumptions.

1 Like

I updated my post with a full script and video just in case

ok I went through your code, there could be several things causing this but I want to start simple.

Looking closely at the video, I’m assuming the countdown numbers on your tool is the “cooldown”, you might be allowing them to fire the skill too early after the previous cast. This could mean that you are firing the function before the previous one ended which might mean that the code is adjusting the animation speed back to 1 right after you just set it to 0.

It doesn’t seem that your code snippet is using a debounce so I’m assuming this is being done somewhere else, make sure the debounce timer lines up with the attack duration so the animations don’t conflict with each other.

1 Like

Thanks, I have another local script that controls the cooldown and the numbers were indicating it. I checked if I was firing the script too early after the previous one, but it happened that my animation started bugging again after I replayed the game and casted my move for the first time, so I don’t think that’s the issue. I also have a global debounce that doesn’t allow moves to be casted during the middle of a move in the part of the script where I fire the GlobalCd remote event.

Yeah, I took another look at the video and there doesn’t seem to be any conflicting code since your walkspeed and jump height don’t seem to reset when your animation speed does, so that was my fault! Apologies for not noticing this earlier.

By any chance is the marker located somewhere near the end of the animation? I remember having an issue with markers that when they are positioned too closely to the end of an animation the animation might end before the marker is even called. If that’s the case try to give an extra 0.1 to 0.5 seconds of animation time after the marker and then simply stop the animation after the casting sequence has ended (instead of adjusting the speed back to 1)

1 Like

Yeah thanks, it turns out that my marker is directly at the end of the animation, and giving it a bit of extra animation time did actually work.

2 Likes