Dances Accumulated

When pressing the dance button, the player simply accumulates them, and does not even stop the one that was pressed for the second time in a row. I do not understand why.

Client
local lPlr = game.Players.LocalPlayer
local char = lPlr.Character or lPlr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
local animations = game:GetService("ReplicatedStorage"):WaitForChild("animations")
local dances = animations:WaitForChild("dances")
local pack_dances = animations:WaitForChild("pack_dances")

local current_anim = nil

script.Parent.MouseButton1Click:Connect(function()
	
	local dance = animations:FindFirstChild(script.Parent.Name, true)
	local loaded_anim = animator:LoadAnimation(dance)
	
	if current_anim ~= loaded_anim then
		if current_anim then
			current_anim:Stop()
		end
		current_anim = loaded_anim
		current_anim:Play()
	elseif current_anim == loaded_anim then
		current_anim:Stop()
		current_anim = nil
	end
	
	local current_animations = animator:GetPlayingAnimationTracks()
	print(current_animations)
	
end)
1 Like