Animation is not playing when I click the button

  1. What do you want to achieve? Keep it simple and clear!
    Basically I am making a script as when player clicks on a button the animation will start playing.
  2. What is the issue? Include screenshots / videos if possible!
    When I press the button the animation is not playing and theres no error in the output.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried looking for solutions via on the internet and the developer forum but I couldn’t find it.
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetId://3333499508"

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)

Any help would be highly appreciated!

1 Like

I think the issue is there, you don’t have anything set up to do anything when the button is pressed, did you mean to this?

script.Parent.MouseButton1Click:Connect(PlayAnimation)

So the button is connected to the PlayAnimation function?

Yes, correct. But there is no error in the output so I’m not quite sure how to fix it.

Try this out perhaps?

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

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)
1 Like

I got this error saying… 20:29:52.194 KeyFrameReached is not a valid member of AnimationTrack “Animation” - Client - LocalScript:13
20:29:52.195 Script ‘Players.adkawoawddwa.PlayerGui.ScreenGui.TextButton.LocalScript’, Line 13 - function PlayAnimation - Studio - LocalScript:13

1 Like

Not sure if this is just a general error when copying over the code but there’s a gap in between LoadAnimation (animation), try LoadAnimation(animation)?

1 Like

Change KeyFrameReached to KeyframeReached, the F isn’t capitalized, I didn’t know that was how the event was written so I left that capitalized

@BmxAlternative I already fixed that and I think it should work after the small change in event name now if it now errored that an AnimationTrack doesn’t have an event with that name

1 Like

It WORKED, THanks man!

1 Like

Anytime! I recommend you mark my post as the solution to help show others that this post is now fixed and immediately show them which post was it! If you have anymore issues don’t be afraid to make another post!

1 Like

Hi, I know this is unrelated. May I ask how long have you been scripting in roblox as a programmer and where did you learn from?

1 Like

Mostly learned a long time ago from an old scripter called PeasFactory/PeasPod, whose videos are sorta outdated now. I’ve been scripting for a few years. Also it would’ve been better to message me instead of replying here again since it’s unrelated to this topic

@EmbatTheHybrid

Hello there kind sir… I was making an animation, stumbled across this post and then wrote this code and got an error in the output and the animation did not play.:confused: I am currently on a group game using a group animation ID. Any ideas as to why this doesn’t work?
Thanks.

invalid animation ID, that animation ID doesn’t exist.

Yeah, but they all exist. It just doesn’t work no matter what I do…

which animation id did you copy from?

First I tried my own animations and then group animations that I have access to. However, neither worked…

try this animation ID {Zombie Run} {616163682}

You want it to play when the textbutton is being clicked or certain key on your keyboard to play the animation?

It would be when a textbutton is being clicked. Let me try your animation and see if it works…

If it does work then your animation ID isn’t valid or doesn’t exist at all according to the output result.