I’m trying to pre-load animations to save up resources and not use up free slots, hwoever when using on tables, it causes them to re-load again when trying to use Play
local function GetAnimations(Character:Model)
local Animator: Animator = Character.Humanoid.Animator
return {
SpecialIdleBayonet = Animator:LoadAnimation(ReplicatedStorage.Assets._tools.Bayonet.Idle),
RegularIdle = Animator:LoadAnimation(ReplicatedStorage.Assets._animations.Idle)
}
end
Player.CharacterAdded:Connect(function(CharacterV)
Character = nil
pcall(function()
for _,v in CharaceterStuff.GUIConnections do
pcall(v.Disconnect, v)
end
end)
table.clear(CharaceterStuff)
Character = CharacterV
CharaceterStuff = GetCharacterStuff(Character)
CharaceterStuff.Animations = GetAnimations(Character)
-- This causes the animation to load again which I don't want
CharaceterStuff.Animations[Character:GetAttribute("IdleName")]:Play()
CharaceterStuff.Humanoid.Died:Once(function()
Character = nil
table.clear(CharaceterStuff)
end)
end)
Everytime you call the GetAnimations() function, indeed will try to load the animations again cause thats what the code in the function does:
SpecialIdleBayonet = Animator:LoadAnimation(ReplicatedStorage.Assets._tools.Bayonet.Idle),
RegularIdle = Animator:LoadAnimation(ReplicatedStorage.Assets._animations.Idle)
That function will be called after the player died and respawned isnt?
Its animator its new and it doesnt have the animations loaded, so, loading them again is actually desirable isnt?
Yes that’s what my point, I’m storing the animations in a table for easy access, but however when calling Play on any of those preloaded animations, it’ll re-load them again, which I don’t want.
Sorry I dont understand what you mean. When player joins or respawn your GetAnimations()
will be called, and load both anims into the Animator and return it as a table to include inside your CharaceterStuff.Animations
table.
Now when you call:
CharaceterStuff.Animations[Character:GetAttribute("IdleName")]:Play()
It wont load the animation again, it will only play it. (btw what do you mean by “load”?) There is no method in the Animator documentation that returns the Animations loaded into the Animator, it only has GetPlayingAnimationTracks(), which will return the Tracks that the Animator is currently playing. How are you checking that supposed double loaded animations?
Now, when the player dies, its Animator gets destroyed, along with whole Character model and Humanoid etc.
Mandatory when the character loads again you need to grab the Animator and load both Anims again to create those 2 Tracks and store it in the table as you are currently doing, so you can Play those tracks whenever you wish by CharaceterStuff.Animations[Character:GetAttribute("IdleName")]:Play()
(just be sure that the Attribute IdleName is added to the Character model again when the new character is created for the player)
But no, Track:Play()
it only plays the animation, do not Load()
the animation again creating a new Track into the Animator