How to use animation events in scripts

Hello!

I’ve been wondering, how would I detect an animation event in a script? For example: on an animation event, a certain sound plays.

1 Like

Use GetMarkerReachedSignal()

https://create.roblox.com/docs/reference/engine/classes/AnimationTrack#GetMarkerReachedSignal

3 Likes

Take a look here.

2 Likes

For more information, you’ll need to add an animation event marker to your animation first. We’ll call ours “SoundEvent”, then export the animation.

The code should look something like this to trigger the event:
This is pseudo-code and not tested but you could make it work

local character  = --Add character pathway
local humanoid = character:WaitForChild("Humanoid")

local sound = --Add pathway

local loadAnimation = Instance.new("Animation")
loadAnimation.AnimationId = --Insert animation Id
local playAnimation = humanoid:WaitForChild("Animator"):LoadAnimation(loadAnimation)

--Play the Animation
playAnimation:Play()

playAnimation:GetMarkerReachedSignal("SoundEvent"):Connect(function)
    sound:Play()
end)
6 Likes

Thank you for all the tactics!!! :smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.