Hey developers. I recently encountered a strange bug where I’m not sure what’s going on. In my game, I need to listen to events (or signals) from AnimationTracks on both the server and client. To accomplish this, I play animations on the server, and on the client I listen to the AnimationPlayed
event of the player’s humanoid to get a reference to the AnimationTrack on the client.
The problem is, even when the animation has stopped and the AnimationTrack has been destroyed on the client, future playbacks of the same animation still trigger the signals. What’s even stranger is that on the server, the signals no longer trigger after the track is destroyed. To explain myself better, here is a reproducible example:
On the server-side, I have the following script:
function playAnimation(animation, humanoid)
local track = humanoid:LoadAnimation(animation)
track:GetMarkerReachedSignal("activateForce"):Connect(function()
print('Event received on server.')
end)
track.Stopped:Connect(function()
track:Destroy()
end)
track:Play()
end
game.Players.PlayerAdded:Connect(function(player)
-- wait for player's character to load
wait(1)
local character = player.Character
if not character then character = player.CharacterAdded:Wait() end
-- load and play the animation (on the server)
local animation = game.ReplicatedStorage.Animation
playAnimation(animation, character.Humanoid)
-- after a while, play it again
wait(2)
playAnimation(animation, character.Humanoid)
end)
And on the client, I have the following script:
local character = game.Players.LocalPlayer.Character
local animation = game.ReplicatedStorage.Animation
character.Humanoid.AnimationPlayed:Connect(function(track)
if track.Animation.AnimationId == animation.AnimationId then
track:GetMarkerReachedSignal("activateForce"):Connect(function()
print('Event received on client.')
end)
track.Stopped:Connect(function()
track:Destroy()
end)
end
end)
Running this game yields the following console output:
Event received on server.
Event received on client.
Event received on server.
Event received on client. (x2)
Trying to play the animation multiple times leads to the event firing on the client every time the animation is played, regardless if it was on a new AnimationTrack. This leads me to believe that the track is possibly cached on the client, so the tracks in the callback to AnimationPlayed are actually the same, but I have no way of verifying this.
Any help is appreciated. I would like the event to fire only once on the client when I play the animation again.
Edit: Would like to add that even explicitly destroying the track on the client does not solve this problem.