How do I play animations on the click of a button?

I tried looking on the developer forums, and on the post I was looking at, there was a solution, I tried the solution and replaced the animation Id with my own animation, it didn’t work.

This is just a LocalScript within a TextButton that is in StarterGui, if I recall correctly, don’t you need to set up a RemoteEvent. I would rather not if I don’t have to but I will if needed to play the animations on a humanoid

Post I was originally looking at for help: Animation is not playing when I click the button

Script is located: StarterGui > ScreenGui > TextButton > LocalScript

code:

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetId://7492057564" -- This is the animation I wanna play

local trackAnimation = nil
local playability = true

function PlayAnimation(animationSource)
	if playability == true then
		local plr = game.Players.LocalPlayer
		trackAnimation = plr.Character.Humanoid:LoadAnimation (animation)

		trackAnimation.KeyFrameReached:Connect(function()

		end)
		trackAnimation:Play()
	end
end
script.Parent.MouseButton1Click:Connect(function(playability)
end)
2 Likes

its just :Connect(PlayAnimation)

2 Likes

So the last line is this instead?

script.Parent.MouseButton1Click:Connect(function(PlayAnimation)
end)
1 Like

@notMundaze: That didn’t work, I have no output, I don’t understand. Am I missing something, all I have is this LocalScript, nothing running on the server.

code:

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetId://7492057564"

local trackAnimation = nil
local playability = true

function PlayAnimation(animationSource)
	if playability == true then
		local plr = game.Players.LocalPlayer
		trackAnimation = plr.Character.Humanoid:LoadAnimation(animation)
		trackAnimation.KeyFrameReached:Connect(function()
		end)
		trackAnimation:Play()
	end
end
script.Parent.MouseButton1Click:Connect(function(PlayAnimation)
end)
1 Like

local character = player.Character or player.CharacterAdded:Wait()

also

local humanoid = character:WaitForChild(“Humanoid”)

1 Like

Where is player defined though?

1 Like

outside of function, local player = game.Players.LocalPlayer

1 Like

Animation still won’t play, I don’t understand, when trying to get the humanoid, the autocorerect wouldn’t appear, maybe that is indicating something? Also, I changed the trackAnimation to plr.Character.humanoid, which is the humanoid variable that I declared at the top.

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetId://7492057564"
local players = game.Players.LocalPlayer
local character = players.Character
local humanoid = character:WaitForChild("Humanoid")


local trackAnimation = nil
local playability = true

function PlayAnimation(animationSource)
	if playability == true then
		local plr = game.Players.LocalPlayer
		trackAnimation = plr.Character.humanoid:LoadAnimation(animation) 
		trackAnimation.KeyFrameReached:Connect(function()
		end)
		trackAnimation:Play()
	end
end
script.Parent.MouseButton1Click:Connect(function(PlayAnimation)
end)
1 Like
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetId://7492057564"
local players = game.Players.LocalPlayer
local character = players.Character
local humanoid = character:WaitForChild("Humanoid")


local trackAnimation = nil
local playability = true

function PlayAnimation(animationSource)
	if playability == true then
		local plr = game.Players.LocalPlayer
		trackAnimation = plr.Character.humanoid:LoadAnimation(animation) 
		trackAnimation.KeyFrameReached:Connect(function()
		end)
		trackAnimation:Play()
	end
end
script.Parent.MouseButton1Click:Connect(PlayAnimation)
2 Likes

That didn’t work, however to provide more context, I did get a stack message:

1 Like

do you want the animation to run locally, or for everyone to see?

1 Like

script.Parent.MouseButton1Click:Connect(PlayAnimation)

1 Like

I fixed the scripting problem, but does this only run on the client? It is in a LocalScript.

1 Like
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetId://7492057564"


local playability = true

function PlayAnimation(animationSource)
	if playability == true then
		local plr = game.Players.LocalPlayer
		local animator = plr.Character.Humanoid:WaitForChild("Animator")
		local trackAnimation = animator:LoadAnimation(animation)
		
		trackAnimation:Play()
	end
end

script.Parent.MouseButton1Click:Connect(PlayAnimation)

Try this:

1 Like

Nope, didn’t work! It’s so weird!

1 Like

Fixed it! This is the code that worked!

:happy1:

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7492057564"

local trackAnimation = nil
local playability = true

local plr = game.Players.LocalPlayer

function PlayAnimation(animationSource)
	if playability == true then
		trackAnimation = plr.Character.Humanoid.Animator:LoadAnimation(animation)
		trackAnimation.KeyframeReached:Connect(function()
			
		end)
		trackAnimation:Play()
	end
end

script.Parent.MouseButton1Click:Connect(PlayAnimation)
5 Likes