I was reading about KeyframeReached event and decided to test it out.
I copied the code Roblox used as an example, followed the instruction on where to place the local script and then tested the script in my game.
Problem
The script worked until I decided to put my own animationID that I created. In my animation I did create Keyframes, but it won’t print in output, and I don’t know why?
The animation I created is very short since I’m just testing this function. Does anyone know why it’s not working with my own animation even though it does have keyframes?
Local Script
local Players = game:GetService("Players")
local player = Players:GetChildren()[1]
local character = workspace:WaitForChild(player.Name)
local humanoid = character:WaitForChild("Humanoid")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://14000879233"
local animTrack = humanoid:LoadAnimation(animation)
animTrack.KeyframeReached:Connect(function(keyframeName)
print("Keyframe reached:" .. keyframeName)
end)
animTrack:Play()
local function listenForAnimationEffects(humanoid) -- would also work for an AnimationController
-- listen for new animations being played on the Humanoid
humanoid.AnimationPlayed:Connect(function(animationTrack)
local keyframeConnection = nil
-- listen for the 'Effect' keyframe being reached
keyframeConnection = animationTrack.KeyframeReached:Connect(function(keyframeName)
if keyframeName == "Effect" then
-- make sure the Humanoid RootPart exists
if humanoid.RootPart then
-- create a basic particle effect
local particles = Instance.new("ParticleEmitter")
particles.Parent = humanoid.RootPart
particles.Rate = 0
particles:Emit(10)
task.delay(2, function()
if particles then
particles:Destroy()
end
end)
end
end
end)
local stoppedConnection = nil
stoppedConnection = animationTrack.Stopped:Connect(function()
-- clean up old connections to stop memory leaks
keyframeConnection:Disconnect()
stoppedConnection:Disconnect()
end)
end)
end
local humanoid = script.Parent:WaitForChild("Humanoid")
listenForAnimationEffects(humanoid)