Dance Animation Script Malfunction

Here is an in-game dance GUI I am creating. I am having an issue with the Stop Animation part-which usually works. However, when I click on the same dance twice or more in a row, it then seems to set the stop animation button to that dance, as you can see in the linked video below. I have also left my script I am using below.


local dances = script.Parent.Frame.ScrollingFrame
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local anim

-- We will pause the script to wait for the chracter to load

repeat wait()
	
until Player.Character
local char = Player.Character
local Humanoid = char:WaitForChild("Humanoid")

-- Creat a Animtion In The Player

local danceAnimation = Instance.new("Animation")
danceAnimation.Parent = char
danceAnimation.Name = "The Robot"

-- Setup the animtion to play

local function setupAnimation (id)
	local selectedId = "rbxassetid://"..id
	danceAnimation.AnimationId = selectedId
	anim = Humanoid:LoadAnimation(danceAnimation)
	anim:Play()
end

-- Stops the current animation playing

script.Parent.Stop.MouseButton1Click:Connect(function()
	if anim then
		anim:Stop()
	end
end)

-- detects when a button is pressed

for _,v in pairs(dances:GetChildren()) do
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			setupAnimation(v.Name)
		end)
	end
end
1 Like

You can use a debounce.

local playing = false
local function setupAnimation (id)
    if playing then return end
    playing = true
	local selectedId = "rbxassetid://"..id
	danceAnimation.AnimationId = selectedId
	anim = Humanoid:LoadAnimation(danceAnimation)
	anim:Play()
end
script.Parent.Stop.MouseButton1Click:Connect(function()
	if anim then
		anim:Stop()
        playing = false
	end
end)

It works however, now I am unable to select The Worm while im doing The Robot, and vice versa. I wouldn’t want my players (if possible), to have to select Stop Animation every time they want to change a dance.

You could also check if the previous animation id is the same as the new one.
(im on mobile so sorry if this doesnt work)

local dances = script.Parent.Frame.ScrollingFrame
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local anim

-- We will pause the script to wait for the chracter to load

repeat wait()
	
until Player.Character
local char = Player.Character
local Humanoid = char:WaitForChild("Humanoid")

-- Creat a Animtion In The Player

local danceAnimation = Instance.new("Animation")
danceAnimation.Parent = char
danceAnimation.Name = "The Robot"

-- Setup the animtion to play

local function setupAnimation (id)
    if anim and string.match(anim.Animation.AnimationId, "%d+") == id then
        return
    end

	local selectedId = "rbxassetid://"..id
	danceAnimation.AnimationId = selectedId
	anim = Humanoid:LoadAnimation(danceAnimation)
	anim:Play()
end

-- Stops the current animation playing

script.Parent.Stop.MouseButton1Click:Connect(function()
	if anim then
		anim:Stop()
	end
end)

-- detects when a button is pressed

for _,v in pairs(dances:GetChildren()) do
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			setupAnimation(v.Name)
		end)
	end
end

Edit: The most recent script you sent (the one i am replying too) seems pretty broken when I tested it. I’m going to revert back to the first edited script you sent me, where the only thing missing was you couldn’t switch between dances. That seems to be the last piece of the puzzle