So, I created an animation that is supposed to play globally while a certain key is held down (panting while holding 'r'). It usually works (meaning, everyone can see the animation and it repeats indefinitely until the key is released); however, sometimes it will simply stop doing the animation or only show it for the player that is holding 'r' (as if it was played locally). My relevant code is shown below. It is all within a local script (I thought this would make it play locally 100% of the time, but it frequently plays for everyone too, so I don't really think that's the problem).
m.KeyDown:connect(function(key)
local player = game.Players.LocalPlayer
if key == "r" and player.Character.Humanoid.WalkSpeed == 16 then -- Holding down 'R'
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
player.Character.Humanoid:UnequipTools() -- unequips all equipped tools.
for i, Tools in pairs(player.Backpack:GetChildren()) do
if Tools:IsA("Tool") then
Tools.Enabled = false
end
end
local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Panting)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 0
game.Players.LocalPlayer.Character.Humanoid.JumpPower = 0
wait(.5) -- kinda a penalty for even deciding to rest, and keeps animation from glitching
animation:Play()
script.Parent.Stats.Frame.StaminaQuantity.StaminaRegen.Disabled = true
script.Parent.Stats.Frame.StaminaQuantity.RestStaminaRegen.Disabled = false
end
end)
If anyone has any idea why this is broken, I'd appreciate it so much. Thanks!

3 comments
when exporting the animation there is an option to have it as a looped animation just check that
I did. Sometimes it loops as it should. Other times, no.
try a while loop on line 3 and once they stop holding r do animation:Stop()