Buggy Animations

What is the issue?
Animations appear fine for the player, but for every other player animations may jitter, repeat, pause, or do other funky things. It is mainly M1 animations that are observably messed up.

What solutions have you tried so far?
Verifying an animation to be played is not already being played, and messing around with priority properties.

-- Client code controlling animations. No other code manages animations.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local RemoteEvent = Remotes:WaitForChild("RemoteEvent")
local localPlayer = Players.LocalPlayer
local animationTracks = {}

local function LoadAnimations(animationIds, animator, character)
	animationTracks[character] = animationTracks[character] or {}
	for name, id in pairs(animationIds) do
		local animation = Instance.new("Animation")
		animation.AnimationId = id
		local track = animator:LoadAnimation(animation)
		animation:Destroy()
		animationTracks[character][name] = track
	end
end

local function PlayAction(name, character)
	if not animationTracks[character] then
		return
	end
	for name, track in pairs(animationTracks[character]) do
		if track.Priority == Enum.AnimationPriority.Action4 then
			track:Stop()
		end
	end
	local track = animationTracks[character][name]
	if track then
		track.Priority = Enum.AnimationPriority.Action4
		track:Play()
	end
end

local function SetIdle(name, character)
	if not animationTracks[character] or not animationTracks[character][name] then
		return
	end

	for name, track in pairs(animationTracks[character]) do
		if track.Looped == true then
			track:Stop()
		end
	end
	local track = animationTracks[character][name]
	track.Looped = true
	track:Play()
end

local function DeleteTracks(character)
	if not animationTracks[character] then
		return
	end
	for name, track in pairs(animationTracks[character]) do
		if track then
			track:Stop()
			track:Destroy()
		end
	end
	animationTracks[character] = nil
end

RemoteEvent.OnClientEvent:Connect(function(arguments)
	local purpose = arguments.purpose
	if purpose == "play animation" then
		local character = arguments.character
		local animationName = arguments.animation
		PlayAction(animationName, character)
	elseif purpose == "stop animation" then
		local character = arguments.character

	elseif purpose == "equip weapon" then
		local weaponData = arguments.data
		local character = arguments.character
		local weldPart = arguments.weldPart
		local humanoid = character:FindFirstChild("Humanoid")
		local animator = humanoid:FindFirstChild("Animator")
		local handle = weldPart:WaitForChild("Handle")
		LoadAnimations(weaponData.animations, animator, character)
		PlayAction("equip", character)
		SetIdle("idle", character)
	elseif purpose == "unequip weapon" then
		local character = arguments.character
		DeleteTracks(character)
	elseif purpose == "start blocking" then
		local character = arguments.character
		PlayAction("block_start", character)
		SetIdle("block_idle", character)
	elseif purpose == "stop blocking" then
		local character = arguments.character
		PlayAction("block_end", character)

		local blockEndTrack = animationTracks[character] and animationTracks[character]["block_end"]
		if blockEndTrack then
			blockEndTrack.Stopped:Wait()
			SetIdle("idle", character)
		end
	end
end)

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		animationTracks[character] = {}
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	local character = player.Character
	if character then
		animationTracks[character] = nil
	end
end)

We’d need to check how the local player’s animations are being played. In your case, they shouldn’t be replicated (their Humanoid should be on the client side). That would explain the glitches. the local animation is probably clashing with the replicated.
If that’s not it, then honestly, I have no idea.

Good call! Firing a remote event to every client to play a single animation on a character apparently isn’t how it works; animations automatically replicate when played on one client. Changing :FireAllClients() to :FireClient(player) fixed it! I think its a bit strange roblox has it set up that way, but whatever. Thank you for your help! :slight_smile: