Animation Tracks delayed when first playing them

Hello. I have a sword handler script that plays hit reactions to the enemy when hitting them, but it seems to be delayed the first time each of the hit reaction tracks play.
After they all get played on one animator, it seems to be fixed.

I tried to preload the animations as well.

-- At the top:
preloadedAnimations = {}
-- FUNCTION TO PRELOAD THE ANIMATIONS
local function preloadReactionAnims(animator)
	local preloadedTracks = {}
	for index, animId in ipairs(properties.HitReactions) do
		local anim = properties.createTrack(animId, animator)
		preloadedTracks[index] = anim
	end
	
	return preloadedTracks
end
--Code Block where I am playing the hit reactions inside of a function --

-- Apply a hit reaction to the enemy
local preloadedTracks = preloadedAnimations[aliveHum.Parent] -- stored on the character of the player/npc
if not preloadedTracks then
	warn("[DEBUG]: Did not find a preloaded track for ", aliveHum.Parent, " possibly R15?")
	return false
end

local hitReactionTrack = preloadedTracks[currentCombo]
if not hitReactionTrack then
	warn("[DEBUG]: Did not find a hitReactionTrack for " .. aliveHum.Parent)
	return false
end

local playedHitTrack = fxManager.playOrStopTrack(hitReactionTrack, true)
playedHitTrack.Ended:Connect(function()
	playedHitTrack:Destroy()
end)
-- Setup for players
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		char:AddTag(PLAYER_TAG) -- add a tag to every new char/player
		
		local hum = char:WaitForChild("Humanoid")
		local animator = hum:WaitForChild("Animator")
		preloadedAnimations[char] = preloadReactionAnims(animator)
	end)
end)
-- Setup for NPC'S
for _, npc in ipairs(npcs) do
	if not npc:IsA("Model") then
		continue
	end
	npc:AddTag(PLAYER_TAG) -- add a tag
	
	-- Preload hit reactions
	local hum = npc:WaitForChild("Humanoid")
	local animator = hum:WaitForChild("Animator")
	
	preloadedAnimations[npc] = preloadReactionAnims(animator)
	print("Preloaded for npc")
end

Any help would be greatly appreciated.

When you test check to see if the animation is loaded into each player and NPC in the workspace (not in the Players Service).
If they aren’t actually added then you know you need to troubleshoot the preloading section of your code.

1 Like

Hello, animations take time to load and for that you should preload them with ContentProvider service.

Here’s what I use in my games, I have an Animations folder under ReplicatedStorage, and it contains every animation in the game, then a local script preloads every animation with ContentProdiver

print("Waiting for animation folder...")
game.ReplicatedStorage:WaitForChild("Animations", 600)
print("Folder loaded. Loading animation instances...")
local t = time()
local AnimationIds = {}
for i, v in pairs(game.ReplicatedStorage.Animations:GetDescendants()) do
	if v:IsA("Animation") then
		table.insert(AnimationIds, v.AnimationId)
	end
end
local AnimationInstances = {}
for i, v in pairs(AnimationIds) do
	local anim = Instance.new("Animation")
	anim.AnimationId = v
	table.insert(AnimationInstances, anim)
end
print("Downloading animations...")
ContentProvider:PreloadAsync(AnimationInstances)
print("Animations fully loaded. Time to load: "..(time()-t).."s")

Remember that with having all your server animation ids be visible for the client it is easier for exploiters to get all your animations instantly, so if it isn’t necessarily needed and it is just to get over the frustration of having to play the animation twice to see it play correctly, I won’t recommend using it.

1 Like