Loading animation issue

Hi, so I am having this issue. Every time i play an animation for the first time there’s a delay or lag on it.


Here’s what I want to get rid

Notice when i equip the weapon there’s a delay on the animation. This only occurs once but in every animation which is kind of annoying. I tried preloading animation which are save in a folder

local ContentProvider = game:GetService("ContentProvider")
local RS = game:GetService("ReplicatedStorage")
local Animations = RS:WaitForChild("Animations")
local Start = os.clock()
ContentProvider:PreloadAsync(Animations:GetDescendants())
local End = os.clock() - Start
print("Took "..End.." To load animations")

-- I have tried different approach such as :  (Scripts are seperated) 
for i,v in pairs(Animations:GetChildren()) do
	ContentProvider:PreloadAsync({v})
end

-- I also tried creating an animation instance incase it doesn't work and yep it didn't 

for i,v in pairs(Animations:GetChildren()) do
	ContentProvider:PreloadAsync({v.AnimationId})
end

I’ve tried looking up some solution to this issue but no can do. They say I only need to preload it but it doesn’t work. Am I preloading the animations right?
image

image
It took 0.001 to weld and play the animation i don’t see any reason for delay
image I
2nd attempt
image

mind giving the code where you go play the animation and load it?

Make sure the animation itself doesn’t have a delay.

If you are loading the animation everytime you click that could be the cause of delay. Load once then play on click

I dont think there is a delay because it took only 0.001 seconds to play which you can see in the image.

function module:equip()
self.Track = self.Animator:LoadAnimation(self.Equipanimation)
self.Track:Play()
end

I wrote this on Mobile I’m the one who made it so i remember.Cant switch to Pc right now.

I dont think its my code that is in fault here because it only occur once but on every animation, yes every animation in the game.

Load this animation when the character is loaded. Not right before you call it.

You mean like this?

function module.new(...)
       Data....
       Data.EquipTrack = Data.Track:LoadAimation(Data.equipanimation)

end

Then on function equip i just need to do Data.EquipTrack:Play()

I’ll try it tomorrow I cant switch to pc rn thanks.

Yeah that’s exactly what I mean. As long as that is called on the animator. Of the current character

1 Like

Add the following line of code before the animation track is played.

repeat task.wait() until AnimationTrack.Length > 0

You’ll need to change the example variable’s name to fit your implementation.

It didn’t work

-- module script
function module.new(...)
	Data..
		Data.EquipTrack = Data.Track:LoadAimation(Data.equipanimation)
end

function module:equip()
	self.EquipTrack:Play(0)
end
--end

--local script

local module = viewmodule.new(...)

UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.One and not eq then
		module:equip()
	end
end)

I added the script and it still has a delay
like in the picture above
(this one)

It waits for the animation track to load (if length is higher than 0) so no animation is being played at the moment so that thing in the picture occur.

This guys have the same problem as me. I don’t know he already fixed it
(a very old thread)

(but mine isn’t a tool)

Maybe not showing the viewmodel until I call then equip animation might work. (I’ll try this one)

1 Like

I know this is an old conversation, and this is also my first reply, but I wish to share my two cents from how I worked around something like this, as it still relates to the core issue. My reply is rather lengthy so bear with me.

You are right that those are the same issues, where despite the animations being preloaded, there is still an underlying thing that isn’t being preloaded (that’s my theory anyway).

My workaround is to have another model, a clone of the player’s character in the workspace, do the preloaded animation(s) at least once, but they must do it when the client player’s character is loaded into the server (or do it every time for each joined character). This is similar to what @CodeAnomaly mentioned, but it just involves another character doing it before anyone else does. So in a server script it might look something like this:

function onPlayerAdded(player)
	player.CharacterAdded:Connect(function(char)
		-- Other CharacterAdded code, if needed
		local startAnim = workspace["animPreload"].Humanoid.Animator:LoadAnimation(Equipanimation)
		startAnim:Play()
		-- Rinse and repeat above two lines with other animations
	end)
end

game:GetService("Players").PlayerAdded:Connect(onPlayerAdded)

where animPreload is the model clone representing another character. Using the multiclient simulation, I was able to observe that in order to rid of the animation lag, each client must “witness” the animation at least once; in other words, the client needs to register the animation being played by another model of its kind. This way, it has the animation, and other non-preloaded aspects of the character animation, already queued on the client before the clients themselves use the animation.

Though this does not have the clone use a tool, I theorize it works the same way; through your device seeing another animation in the same game instance, it will register it, making your animation instant (hopefully).

This seems like a roundabout way of doing it and a somewhat extra thing to do, but I think this in addition to PreloadAsync() is the best way to make sure that the initial animation lag is taken care of. I would hide the model in a place unseen (like under the map), or make it invisible so it doesn’t awkwardly stand out. Perhaps someone else will come along and build upon this idea or make it more concrete, but for now this is my solution. Hope this helps you or anyone else stuck on this issue.

1 Like

Thanks for replying and sorry about the late reply.
It’s all good now. I tried publishing the game and played it on the actual roblox and it’s working fine. The issue only occur on studio for some reason. I forgot to add a comment about this, sorry about that I’ve been busy lately.

1 Like