How to run an animation onClick?

I’m attempting to make it so that when you click while holding a tool, the animation runs.

I checked the wiki and didn’t quite understand what was going on since this is my first time working with animations.

Could anyone help me figure out how to run the animation?

13 Likes

After creating the animation instance and setting it’s ID per the API, you run the following code


local animation = -- have this set to the animation instance
local track = [HUMANOID]:LoadAnimation(animation)

-- tool activation logic
track:Play()

-- in the event that the animation is looped
-- tool deactivation logic
track:Stop()

9 Likes

There are a variety of ways to check for a player’s click, however the best option is to use UserInputService. However it only works in LocalScripts, so that’s what the script will need to be.

Basically you check when a player begins input (e.g. starts holding down a key), then check what type of input they are doing.

local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent

UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
        -- play animation
    end
end)

To play an animation, you do what Kiansjet said above.

How it works is you use the LoadAnimation method found in Humanoid, giving it the Animation object that has the AnimationId and other data.
This method returns an AnimationTrack object that you can use to control the animation – play it, stop it etc.

Here’s an example:

local Player = game.Players.LocalPlayer -- this also only works in LocalScripts
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local Animation = Instance.new("Animation") -- create a new animation object
Animation.AnimationId = "rbxassetid://00000" -- put your animation id over the zeroes

local Track = Humanoid:LoadAnimation(Animation)
Track:Play()
Track:Stop()
34 Likes

On top of never working with Animations, I’ve never used UserInputService before either.

So I have 0 clue as to what I should do, but I know that the way I set it up is wrong:

local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
        local Animation = Instance.new("Animation") 
        Animation.AnimationId = "rbxassetid://1630162920" 
        local Track = Humanoid:LoadAnimation(Animation)
        Track:Play()
    end
end)

Could someone explain what I’m doing wrong?

3 Likes

Is this a LocalScript?

UserInputService can only be used in a LocalScript – otherwise everything you are doing is fine

For optimisation, you’d want to move everything except Track:Play() out of the InputBegan function.

1 Like

I tried that, and it still didn’t work. It’s a local script, inside the tool. Is that correct?

1 Like

Yep. Does it have any errors in the output?

1 Like

No errors. That’s why it’s kinda odd to me. The game is Filtering Enabled (or Non-Experimental)

1 Like

Try printing in areas of your code to see where it reaches.

e.g. during the input began function, before getting the humanoid, etc.

2 Likes

Added in prints, and everything is running, but for some reason the animation doesn’t animate.

It does get cloned so you are able to pick it up, does that make a difference to the script?

1 Like

No, that shouldn’t have an effect.

Sounds like it might be an issue with your animation – have you set the animation to play over the core animations?

3 Likes

Probably not. How do I do that?

1 Like

In the animation editor. I don’t know why we can’t change them real-time, but you have to change it when editing the animation.

5 Likes

Thanks a bunch man!! I set the priority and it works!! :smiley:

3 Likes

Is this in the tool or starter player scripts? Etc…

You can do either, but if you’re using tools it should be inside the tool. StarterPlayerScripts would be where you do things that aren’t tools.

2 Likes

In case anyone visited this thread expecting it to be about an animation gui I’d recommend the GetAnimations API method.
https://developer.roblox.com/en-us/api-reference/function/KeyframeSequenceProvider/GetAnimations

do
	local Enumeration = Enum
	local Game = game
	local KeyframeSequenceProvider = Game:GetService("KeyframeSequenceProvider")
	local MarketplaceService = Game:GetService("MarketplaceService")
	local Players = Game:GetService("Players")
	local LocalPlayer = Players.LocalPlayer
		
	local AnimationGui = Instance.new("ScreenGui")
	AnimationGui.Name = "AnimationGui"
	AnimationGui.ResetOnSpawn = false
	
	local AnimationFrame = Instance.new("Frame")
	AnimationFrame.Name = "AnimationFrame"
	AnimationFrame.Position = UDim2.new(0, 50, 0.5, 0)
	AnimationFrame.Size = UDim2.new(0, 250, 0, 150)
	AnimationFrame.Style = Enumeration.FrameStyle.RobloxSquare
	AnimationFrame.Visible = false
	AnimationFrame.Parent = AnimationGui
	
	local function OnGuiTextButtonMouseClick(GuiTextButton)
		local State = (GuiTextButton.Style == Enumeration.ButtonStyle.RobloxButton)
		GuiTextButton.Style = if State then Enumeration.ButtonStyle.RobloxButtonDefault else Enumeration.ButtonStyle.RobloxButton
		GuiTextButton.Text = if State then "👈" else "👉"
		AnimationFrame.Visible = State
	end
	
	local GuiTextButton = Instance.new("TextButton")
	GuiTextButton.Name = "GuiTextButton"
	GuiTextButton.Font = Enumeration.Font.GothamMedium
	GuiTextButton.Position = UDim2.new(0, 0, 0.5, 0)
	GuiTextButton.Size = UDim2.new(0, 50, 0, 50)
	GuiTextButton.Style = Enumeration.ButtonStyle.RobloxButton
	GuiTextButton.Text = "👉"
	GuiTextButton.TextColor3 = Color3.new(1, 1, 1)
	GuiTextButton.TextSize = 48
	GuiTextButton.MouseButton1Click:Connect(function() OnGuiTextButtonMouseClick(GuiTextButton) end)
	GuiTextButton.Parent = AnimationGui
	
	local AnimationsFrame = Instance.new("ScrollingFrame")
	AnimationsFrame.Name = "AnimationsFrame"
	AnimationsFrame.AutomaticCanvasSize = Enumeration.AutomaticSize.Y
	AnimationsFrame.BackgroundTransparency = 1
	AnimationsFrame.BorderSizePixel = 0
	AnimationsFrame.CanvasSize = UDim2.new(0, 0, 0, 0)
	AnimationsFrame.ScrollBarImageColor3 = Color3.new(1, 1, 1)
	AnimationsFrame.ScrollBarThickness = 10
	AnimationsFrame.Size = UDim2.new(1, 0, 1, 0)
	AnimationsFrame.VerticalScrollBarInset = Enumeration.ScrollBarInset.ScrollBar
	AnimationsFrame.Parent = AnimationFrame
	
	local UIListLayout = Instance.new("UIListLayout")
	UIListLayout.Parent = AnimationsFrame
	
	local Success, Result = pcall(KeyframeSequenceProvider.GetAnimations, KeyframeSequenceProvider, LocalPlayer.UserId)
	if not Success then warn(Result) return end
	
	local AnimationIds = {}
	while true do
		local Page = Result:GetCurrentPage()
		for _, Item in ipairs(Page) do
			table.insert(AnimationIds, Item)
		end
		if Result.IsFinished then break end
		Result:AdvanceToNextPageAsync()
	end
	
	local function OnAnimationStopped(AnimationTextButton)
		AnimationTextButton.Style = Enumeration.ButtonStyle.RobloxButton
	end
	
	local Tracks = table.create(#AnimationIds)
	
	local function OnAnimationTextButtonMouseClick(AnimationTextButton, AnimationId)
		local Character = LocalPlayer.Character
		if not Character then return end
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		if not Humanoid then return end
		local Animator = Humanoid:FindFirstChildOfClass("Animator")
		if not Animator then return end
		local State = (AnimationTextButton.Style == Enumeration.ButtonStyle.RobloxButton)
		local Track
		for _, _Track in ipairs(Tracks) do
			local Id = string.match(_Track.Animation.AnimationId, "^rbxassetid://(%d+)")
			Id = tonumber(Id)
			if Id == AnimationId then Track = _Track break end
		end
		if State then
			AnimationTextButton.Style = Enumeration.ButtonStyle.RobloxButtonDefault
			for _, Track in ipairs(Tracks) do
				if Track.IsPlaying then Track:Stop() break end
			end
			
			if not Track then
				local Animation = Instance.new("Animation")
				Animation.AnimationId = "rbxassetid://"..AnimationId
				Track = Animator:LoadAnimation(Animation)
				table.insert(Tracks, Track)
				Track.Stopped:Connect(function() OnAnimationStopped(AnimationTextButton) end)
			end
			Track:Play()
		else
			if Track then Track:Stop() end
		end
	end
	
	for _, AnimationId in ipairs(AnimationIds) do
		local Success, Result = pcall(MarketplaceService.GetProductInfo, MarketplaceService, AnimationId)
		if not Success then warn(Result) continue end
		
		local AnimationTextButton = Instance.new("TextButton")
		AnimationTextButton.Name = "AnimationTextButton"
		AnimationTextButton.Font = Enumeration.Font.GothamMedium
		AnimationTextButton.Size = UDim2.new(1, 0, 0, 50)
		AnimationTextButton.Style = Enumeration.ButtonStyle.RobloxButton
		AnimationTextButton.Text = Result.Name
		AnimationTextButton.TextColor3 = Color3.new(1, 1, 1)
		AnimationTextButton.TextSize = 18
		AnimationTextButton.MouseButton1Click:Connect(function() OnAnimationTextButtonMouseClick(AnimationTextButton, AnimationId) end)
		AnimationTextButton.Parent = AnimationsFrame
	end
		
	local function OnCharacterAdded(Character)
		table.clear(Tracks)
	end
	
	LocalPlayer.CharacterAdded:Connect(OnCharacterAdded)
	local Character = LocalPlayer.Character
	if Character then task.spawn(OnCharacterAdded, Character) end
	local PlayerGui = LocalPlayer:FindFirstChildOfClass("PlayerGui") or LocalPlayer:WaitForChild("PlayerGui", 10)
	if not PlayerGui then return end
	AnimationGui.Parent = PlayerGui
end

Here’s an example using ‘261’ (Shedletsky) as the user ID.

image

1 Like