AnimationTrack "FrameChanged" event?

I don’t know if something is going over my head here, because this seems like an obvious feature that should be in Studio already, but I’m curious if there’s an event in AnimationTracks that can detect whenever a playing animation has moved along its timeline, as in the frame it’s playing has changed. “KeyframeReached” and “GetMarkerChangedSignal” (to my knowledge) do not fit these requirements, as they both only fire for specific keyframes, defined by a string parameter.

The event would also need to return the frame of the animation that it changed to.

Perhaps there is a different way to do this, but this is essentially what I need:

local AnimationTrack = Humanoid:LoadAnimation(WalkAnimation)

AnimationTrack.FrameChanged:Connect(function(NewFrame)
     print("The animation is now on frame ".. NewFrame)
end)

AnimationTrack:Play()

Forgive my ignorance if there is already something like this and it went by me. Thanks.

1 Like

Why do you need to check every frame though? That doesn’t really make a lot of sense to me. What’s wrong with the events that are currently available?

local player = game.Players:GetChildren()[1]
local character = game.Workspace:WaitForChild(player.Name)
local humanoid = character:WaitForChild("Humanoid")
 
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://437855404"
local animTrack = humanoid:LoadAnimation(anim)
 
animTrack.KeyframeReached:Connect(function(keyframeName)
	print("Keyframe reached:"..keyframeName)
end)
animTrack:Play()

In the post he specifically says he does not want to use those events, did you even bother reading the post?

Second, KeyFrameReached is deprecated, using deprecated methods is bad practice.

Third thing, if you even read the documentation, the event only fires for keyframes that have their name changed.

1 Like

I am using animations as a way to have camera movement for my FPS system. Since each animation can have correlating camera movement data, it would be nice to be able to move the camera directly in-sync with each keyframe. Currently, I have the camera movements in a RenderStepped loop that starts at the same time I play the animation, so it can sometimes de-sync from the animation if, for example, the game lags.

Here is an example of an animation + its corresponding camera movement.
https://i.gyazo.com/b6d52e431c86223ec3b924721e5fceff.mp4

Because the number of frames in the animation is identical to the number of times the camera should move, I could easily sync them up with the event I articulated in the original post.

The closest thing I could think of would be using the TimePosition property, and hooking up a changed signal to it.

AnimationTrack:GetPropertyChangedSignal("TimePosition"):Connect(function() 
    -- code
end)

But I suggest you just change your code to work with the animation events built-in.

I thought this was a good idea, but it doesn’t seem like the TimePosition property triggers a changed signal.

image

This is the code I have for playing the reload animation. Nothing gets printed into the output. I also tried spawning the event after the :Play() line, but that didn’t work either.