How would I add animations to this script?

  1. What do you want to achieve?
    I’m making a flying script that is FilteringEnabled

  2. What is the issue?
    because the Local Script uses

while true do
RemoteEvent:FireServer()

it fires the remote event repeatedly, and an animation is being played for every millisecond they player is holding down the W key which causes the animations to go over the maximum limit of 256

  1. What solutions have you tried so far?
    I Tried adding a value that checks if the player is already playing an animation but the animation doesn’t stop
if input == "Forward" then
		if HumaoidRP:FindFirstChildOfClass("BodyVelocity") then
			HumaoidRP:FindFirstChildOfClass("BodyVelocity"):Destroy()
		end
		if HumaoidRP:FindFirstChildOfClass("BodyGyro") then
			HumaoidRP:FindFirstChildOfClass("BodyGyro"):Destroy()
		end
		HumaoidRP.Anchored = false



---------------------------------Checking for IsPlayingAnim.value--------------------------------------------------
		if script.IsPlayingAnim.Value == false then
			script.IsPlayingAnim.Value = true
			PlayAnim:Play()-- play the animation
		end
--------------------------------------------------------------------------------------------------------------------------









----------------------------------------------Stop playing the animation--------------------------------------------------------------------
	if input == "Forward Ended" then
		print("Forward Ended")
		if HumaoidRP:FindFirstChild("ForwardMovement") then
			HumaoidRP.ForwardMovement:Destroy()
			HumaoidRP.Anchored = true
		end	
		PlayAnim:Stop()
	end
--------------------------------------------------------------------------

I formatted the script so I can be easier to find the problem