Animation won't stop

Hello! I am making an emote system, where when the player clicks on the GUI object the animation plays.

However, when the player presses the GUI, whatever dance animation is playing stops, but this is not working as it doesn’t stop it! It just overrides the old dance animation.

There are no errors in the output.

Can anyone help me? I would greatly appreciate it!

Thank you.

local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local hum = character:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")

local PlayerMod = require(plr.PlayerScripts:WaitForChild("PlayerModule"))
local Controls = PlayerMod:GetControls()

local animations = {
	["Sturdy"] = animator:LoadAnimation(plr.PlayerGui:WaitForChild("Emotes"):WaitForChild("Animations").Sturdy),
	["Insanity"] = animator:LoadAnimation(plr.PlayerGui:WaitForChild("Emotes"):WaitForChild("Animations").Insanity)
}

local function stopAnims()
	for _, v in ipairs(animations) do
		v:Stop()
	end
end

local db = false

for _, v in ipairs(script.Parent:GetChildren()) do
	if v:IsA("ImageButton") then
		v.MouseButton1Click:Connect(function()
			if not db then
				stopAnims()
				local anim = table.find(animations, v.Name)
				anim:Play()
				Controls:Disable()
				db = true
			elseif db then
				stopAnims()
				local anim = table.find(animations, v.Name)
				anim:Stop()
				Controls:Enable()
				db = false
			end
		end)
	end
end
local function stopAnims()
    for _,v in pairs(animator:GetPlayingAnimationTracks()) do
        v:Stop()
    end
end

this stops all animations playing by the animator, you were close!
Animator:GetPlayingAnimationTracks() creates a table of all the playing animations and you just use an advanced for loop to loop through these and end them.

here is the documentation: https://create.roblox.com/docs/en-us/reference/engine/classes/Animator#GetPlayingAnimationTracks

Hope this helps! :upside_down_face:

1 Like

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