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.
You didn’t set the activated variable to false when you stop the animation.
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.
Use if boolean then (for true) and if not boolean then (for false) because it’s easier to write.
thank you for the quick reply! I tried doing this, but it didn’t work. Sorry I’m quite new to scripting
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
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.
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
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.