#Any better way to preload animations

1 messages · Page 1 of 1 (latest)

burnt merlin
#

Is there any proper and as fast and this way to preload anims

local Debris = game:GetService("Debris")
local Players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local anims = rs.Animations.Weapons.Katana.Main

local function getAnimator(char)
local humanoid = char:FindFirstChildOfClass("Humanoid")
if not humanoid then return nil end

local animator = humanoid:FindFirstChildOfClass("Animator")
if not animator then
    animator = Instance.new("Animator")
    animator.Name = "Animator"
    animator.Parent = humanoid
end

return animator

end

local preloaded = {}

local function PreloadAnimations(char, animList)
local animator = getAnimator(char)
if not animator then return end

for _, animId in ipairs(animList) do
    if not preloaded[animId] then
        local animation = Instance.new("Animation")
        animation.AnimationId = animId

        local track = animator:LoadAnimation(animation)
        track:Play()
        track:Stop()
        animation:Destroy()

        preloaded[animId] = true
    end
end

end

local AnimationIdsToPreload = {
"rbxassetid://14984130659", --walk
"rbxassetid://14984120951", --idle
"rbxassetid://128104128549543",--katana equip
"rbxassetid://119085738060242",-- katana idle
"rbxassetid://139314306808113" -- Rapier idle
}

Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
-- Delay slightly to ensure character fully loads
char:WaitForChild("Humanoid")
task.wait(0.1)

    -- Preload animations for this character
    PreloadAnimations(char, AnimationIdsToPreload)
end)

end)

storm ivy
#

How're you going to play the animations?

rotund hatch
#

Rn ur script just creates unnecessary work for the server

burnt merlin
burnt merlin
storm ivy
#

Whats wrong with the current approach?

burnt merlin
storm ivy
#

no need to load the server with that

#

animations are meant to be client-side

burnt merlin
#

Everything else is alr?

storm ivy
#

gimme a sec, ill take another look

#

You're storing the animationTrack unnecessarily if this script only cares about loading the animations. You can just do Animator:LoadAnimation(Animation) and carry on.

#

Also, instead of using task.wait(), just use the Player.CharacterAdded event as some players may take longer than 0.01 second for their character to load

#

Everything else is alright. You can always switch to LocalScript with minor adjustments, though this approach works fine too.

#

It has unnoticeable overhead on the server