Animation wont stop

Hi. I am trying to make a crouch button for mobile compatiblity but the animation doesnt stop after the button is clicked twice.

Button click fires a remoteevent which is this:

local event = game.ReplicatedStorage.RemoteEvents.PlayCrouchAnim
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://108365018559262"

event.OnServerEvent:Connect(function(plr, state)
	local char = plr.Character
	if not char then return end

	local hum = char:FindFirstChild("Humanoid")
	if not hum then return end

	local animator = hum:FindFirstChild("Animator") or Instance.new("Animator", hum)
	local animTrack = hum:FindFirstChild("CrouchAnimTrack")

	-- Load animation if it's not already loaded
	if not animTrack then
		animTrack = animator:LoadAnimation(animation)
		animTrack.Name = "CrouchAnimTrack"
	end

	if state == "activate" then
		hum.HipHeight = -1
		hum.WalkSpeed = 6
		animTrack:Play(0.2)
		animTrack:AdjustSpeed(0)

		-- Update animation speed using RenderStepped
		game:GetService("RunService").Heartbeat:Connect(function()
			local speed = hum.MoveDirection.Magnitude
			animTrack:AdjustSpeed(speed > 0 and 1 or 0)
		end)

	elseif state == "deactivate" then
		hum.HipHeight = 0
		hum.WalkSpeed = 16

		-- Stop animation
		if animTrack then
			animTrack:Stop()
		end
	end
end)

I tried geting AI to fix the issue but it didnt work. And its not a problem with the deactivate state not being fired cuz the hipheight and walkspeed does go back to 0 and 16 so idk why it wont disable the animation.

1 Like

what is the state being set to

1 Like

also wouldn’t you want to stop the animator instead of the animation?

The states work fine cuz the hipheight and walkspeed resets fine but the animtrack wont stop playing

Idk what the difference is should i try it this way?

1 Like

this was wrong

1 Like

How would i do it this way?
asddsadasdasd

1 Like

I’m not that good with animation scripting, but I could look at the documents

2 Likes

Shouldn’t it be if AnimTrack?

1 Like

To be honest i really don’t know much about this kind of thing so idk

1 Like

Yeah, this way of doing this just feels… so weird to me so im just confused l

2 Likes

The animation will not stop because the function is being called again, that is, it is a new variable for a new animation.
To stop the desired animation you need to get the animation from Animator and stop it. (GetPlayingAnimationTracks)

1 Like

This worked thank you very much!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.