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.