Animation Toggle

Hello! I was wondering if anybody could help me!

I am trying to make an animation toggle GUI!

It works well but when I click the GUI button again, the animation track just starts again rather than stopping. I have tried animationTrack:Stop() too, but it didnt work either.

(not getting any errors)

Any help is greatly appreciated!
image|628x353

There’s a couple things I want to point out.

  1. You didn’t set the activated variable to false when you stop the animation.
  2. The second elseif condition should be elseif activated == true then because the first one was if activated == false to play the animation and the variable was set to true.
  3. Use if boolean then (for true) and if not boolean then (for false) because it’s easier to write.
1 Like

thank you for the quick reply! I tried doing this, but it didn’t work. Sorry I’m quite new to scripting :confused:

Fashionable.MouseButton1Click:Connect(function()

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://3333331310"
local animationTrack = humanoid:LoadAnimation(animation)

if activated then

	animationTrack:Play()

	if not activated then

		animationTrack:Destroy()

	end

end

end)

Well, first off, your second if statement is seated inside the first, so activated in this case would have to be true before it could be found false and destroyed, which isn’t possible.

1 Like

sorry, I am pretty new to scripting so idk how to fix it LOL

the original script is

Fashionable.MouseButton1Click:Connect(function()

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://3333331310"
local animationTrack = humanoid:LoadAnimation(animation)

if activated == false then
	activated = true
	
	animationTrack:Play()
	
else activated = false
	
	animationTrack:Destroy()
	
end

end)

Fashionable.MouseButton1Click:Connect(function()

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://3333331310"
local animationTrack = humanoid:LoadAnimation(animation)

if not activated then
	activated = true
	
	animationTrack:Play()
	
elseif activated then
	
    activated = false
	animationTrack:Stop()
	
end

end)

(sorry if the indentation sucks lol)

Fashionable.MouseButton1Click:Connect(function()
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://3333331310"
local animationTrack = humanoid:LoadAnimation(animation)

if activated == false then
	
	activated = true	
	animationTrack:Play()
	
elseif activated == true then
	
	animationTrack:Destroy()
	activated = false
end
end)

Here is a fixed script. I would like to note however that Humanoid:LoadAnimation is deprecated. You should be using Animator (Animator | Documentation - Roblox Creator Hub) instead.

thank you for the reply, however this didn’t work. when i click it again, it just restarts the animation and doesnt stop

thank you! unfortunately the script didnt work :confused: it just restarts the animation rather than stopping it

Hmm. Where is the activated variable defined? In the function or outside of it?

outside of the function. should I define it inside?

edit: tried to define it inside the function but that didnt work either

No, just wondering because if it was inside that would make the animation restart like you said. Maybe some other function is altering the variable?

that’s the only function in the script

try putting

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://3333331310"
local animationTrack = humanoid:LoadAnimation(animation)

outside of the function

Also recommend using the animator instead of the humanoid for :LoadAnimation

just tried that and the same issue is occuring. loaded the animation onto the animator too

local activated = false

Fashionable.MouseButton1Click:Connect(function()
 local animation = Instance.new("Animation")
 animation.AnimationId = "rbxassetid://3333331310"
 animation.Parent = script
 local animationTrack = humanoid:LoadAnimation(animation)

if activated == false then
	
	activated = true	
	animationTrack:Play()
	
elseif activated == true then
	
	animationTrack:Destroy()
	activated = false
end
end)

Try that.

same issue, i dont actually think its doing anything on the second click…

Check the Output. Best way to fix the problem.

no issues in output, i highlighted that in the original post