How to stop this Animation lag on start?

Does anyone know how to fix this animation lag when the tool is equipped:

The animation lag only happens when the animation is first played.

This is the LocalScript I used to play the animations:

local Player = game:GetService("Players").LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Animator = Humanoid:WaitForChild("Animator")

local AnimationsFolder = script:WaitForChild("Animations")

local R15Folder = AnimationsFolder:WaitForChild("R15")
local R6Folder = AnimationsFolder:WaitForChild("R6")

local Tool = script.Parent

Tool.Equipped:Connect(function()
	local R15ToolEquipAnimation = R15Folder:WaitForChild("R15ToolEquip")
	local R6ToolEquipAnimation = R6Folder:WaitForChild("R6ToolEquip")

	local ToolEquipAnimation

	if Humanoid.RigType == Enum.HumanoidRigType.R15 then
		ToolEquipAnimation = Animator:LoadAnimation(R15ToolEquipAnimation)
	elseif Humanoid.RigType == Enum.HumanoidRigType.R6 then
		ToolEquipAnimation = Animator:LoadAnimation(R6ToolEquipAnimation)
	end

	ToolEquipAnimation:Play()

	ToolEquipAnimation.Stopped:Connect(function()
		local R15ToolIdleAnimation = R15Folder:WaitForChild("R15ToolIdle")
		local R6ToolIdleAnimation = R6Folder:WaitForChild("R6ToolIdle")

		local ToolIdleAnimation

		if Humanoid.RigType == Enum.HumanoidRigType.R15 then
			ToolIdleAnimation = Animator:LoadAnimation(R15ToolIdleAnimation)
		elseif Humanoid.RigType == Enum.HumanoidRigType.R6 then
			ToolIdleAnimation = Animator:LoadAnimation(R6ToolIdleAnimation)
		end

		ToolIdleAnimation:Play()
	end)
end
1 Like

Looking at the video, it is not clear what is expected or unexpected.
Based on what you are saying though, about the animation only “lagging” when first used, probably means that the animation is data is being fetched and loaded. You could try pre-loading the animation in some way. Load the animation when the script is initialized, but don’t play it until you need to.

1 Like

I have used a LocalScript inside ReplicatedFirst to preload my animations and the animations play after the preloading has complete but I am still getting the lag. If this helps this is inside the LocalScript:

local ContentProvider = game:GetService("ContentProvider")

local Animations = script:WaitForChild("Animations")

print("Preloading Animations...")

ContentProvider:PreloadAsync(Animations:GetChildren())

print("Animations Loaded!")

If I’m being honest, I’ve had mixed results when working with PreloadAsync. I’d recommend instead moving these lines of your code to be outside and above the equipped function:

	if Humanoid.RigType == Enum.HumanoidRigType.R15 then
		ToolEquipAnimation = Animator:LoadAnimation(R15ToolEquipAnimation)
	elseif Humanoid.RigType == Enum.HumanoidRigType.R6 then
		ToolEquipAnimation = Animator:LoadAnimation(R6ToolEquipAnimation)
	end
1 Like