How do I script a button to do a animation?

Hi Developers,

So, I want to make a button do an animation. But How exactly ?

I made plently of animations on the past, but how do I make button execute the animation and make you do it?

I have a Image Button and a Animation, but how do I script image button to do my animation?

Thank you for reading this, and if you know how to do this please let me know.

I was thinking somewhere in the script I would have to require:

local animID = -- (the ID here)
  

Don’t take scripting advice from me, I could be VERY wrong. i was asuming.

You’ll want to put this script in the button, and make an Animation instance and parent it to the script.

--LocalScript
local Players = game:GetService("Players");

local plr = Players.LocalPlayer;
local char = plr.Character;
local hum = char:WaitForChild("Humanoid");

local anim = script:WaitForChild("Animation"); --Put an animation in this script
local track = hum:LoadAnimation(anim); --Loads the animation

script.Parent.MouseButton1Down:Connect(function()
	if track.IsPlaying() then
		track:Stop(); --Stops the animation if it's already playing
	else
		track:Play(); --Plays the animation if not
	end
end)

Thank you so much! I have a few questions though.

  • Whats an “Animation Instance”?

  • That script you just posted, how do I edit up a bit to make it that, if you click it again it stops doing the animation?

  • The brackets where you said:

track:Play(); --Plays the animation if not

Do I put anything in those brackets?

In roblox, an instance is just an object. An animation instance looks like this
image
And this script already stops the animation if clicked while it’s playing.

1 Like

also keep in mind @AxstinPlayz you do not have to do ; hes only doing it because of some other type of language hes most likely used to.

1 Like