Is there a way to stop a dancing animation when playing a different one?

I’ve created a simple GUI system with dances. The only issue i have is, when i select a dance , i can select another one without stopping the first one. I searched on Dev Forum, Dev Hub and Youtube and i couldn’t find anything. Is there a way to stop a dancing animation when playing a different one?

https://gyazo.com/716e4cb7487e910061df8b87d2f92a6b

local player = game.Players.LocalPlayer
local character = player.Character
repeat wait()
	character = player.Character
until character
local hum = character:WaitForChild("Humanoid")
local emote = hum:LoadAnimation(script.Parent.Emote)
playing = false

script.Parent.MouseButton1Click:connect(function()
	if playing == false then
		emote:Play()
		hum.WalkSpeed = 0
		hum.JumpPower = 0
		playing = true
	elseif playing == true then
		emote:Stop()
		hum.WalkSpeed = 16
		hum.JumpPower = 50
		playing = false
	end
end)

Each button has this script in it and i know when i stop an emote from playing the WalkSpeed and the JumpPower will go back to their normal values, even if there is another animation playing, which is why i want for all the animations to stop when playing a new one.

image

At first i wanted to do a script with all the dances Emote defined and when a specified emote is playing, the other ones will stop but realised that this will take a lot of time, since i plan on adding many more dances in the future.

Note that i am a beginner scripter and i might not know all the ways (harder or not) possible to do that

Thank you for helping <3

Not sure if you have fixed this, but I would suggesting using a variable like prev

Declare a local prev

and every time the animation is changed, if prev is not nil then prev:Stop() animation. Then, play the new animation, and set the prev variable to the new animation that was just played.

Thanks so much for the tip but right now i am trying to make the animation stops if a player is moving both on pc or mobile.

Well you could set up a listener for a humanoid.Changed event
If the humanoid is in a running state, then you just stop playing any animation.

Can you give me an example? I am still new to scripting so i don’t know most of the things. I just understood how ContextAction works and i thought that would work.

Edit : I think i got it, i am looking on DevHub and i saw Humanoid.Changed. Thank you so much :heart:. I might reply again if i have an issue that i cannot find a solution.

@jimbot220
Alright so i did as it told me and here is where i got stuck. The animation stops when a player jumps but it doesn’t stop when the player moves. I tried both with Running and RunningNoPhysics.

hum.StateChanged:Connect(function(jumpState , runState)
	if jumpState == Enum.HumanoidStateType.Jumping then
		emote:Stop()
		playing = false
	elseif runState == Enum.HumanoidStateType.Running then	
		emote:Stop()
		playing = false
	end
end)

Is Running the correct enum item?

You could keep a variable of the current animation .
then if the player picks another one then you could check the variable and turn it off.

According to the wiki, the two parameters are function(oldState, newState)

So you are not properly referencing the two parameters in your function.

The two parameters of the function can be named however you want as long as you use them correctly. As i said, the animation stops when i jump but it doesn’t stop when i run. I am trying to make so when a player moves/jumps, the dancing animation he is currently playing will stop, @No_b2. But thank you for that advise.:heart:

You’re right that they could be named however you want, but you are also using them incorrectly.

The oldState and newState represent the two parameters passed through the function when it is connected by the event.

Please see the parameters section of the following link below:

I am extremely sorry for my misunderstanding. I thought oldState and newState we’re just name examples. Thank you for clarifying this for me :heart: