Idle Anim playing when it shouldnt be whilst flying

I’m new to scripting but have managed to create a semi-decent flight script.

I currently have the idle and forward-flying anims complete, however, when activating flight whilst running, the forward animation doesn’t active and the character instead just flies with the idle one playing.

This happens because the forwards animation only activates when both flying = true and movement.forward > 0, and since running means that I’m already moving forwards before turning on the flight, the script only recognises 1/2 of the requirements.

As I said, I’m new to scripting and am just wondering what the best way to get around this would be

The section of code responsible for activating the animation:

if (isFlying) then
	local isMovingForward = movement.forward > 0;
	if isMovingForward == true then
		idleAnim:Stop();
		moveAnim:Play();
		fallAnim:Stop();
		fastAnim:Stop();
		leftAnim:Stop();
		rightAnim:Stop();
	else 
		lastAnim:Play();
		moveAnim:Stop();
		fallAnim:Stop();
		fastAnim:Stop();
		leftAnim:Stop();
		rightAnim:Stop();
	end
end 		

Thanks! :slight_smile:

To deactivate all playing animation tracks use:

local function StopAnimations()
	 local PlayingAnimations = Humanoid:FindFirstChildWhichIsA("Animator"):GetPlayingAnimationTracks()

	for _, track in ipairs(PlayingAnimations) do
		track:Stop()
	end
end

I’m guessing that moveAnim is your flying animation. In this case you can then do:

local function StopAnimations()
	 local PlayingAnimations = Humanoid:FindFirstChildWhichIsA("Animator"):GetPlayingAnimationTracks()

	for _, track in ipairs(PlayingAnimations) do
		track:Stop()
	end
end

if isFlying then
	StopAnimations()
	if (Humanoid.MoveDirection.Magnitude > 0) then -- If the player or character is moving, as I don't know the code you use to determine what direction the player is moving in
		moveAnim:Play()
	else
		ideAnim:Play()
	end
end

Also, as a side note, if you are using Humanoid | Roblox Creator Documentation to determine if the player is moving then just know that it is not replicated to the client, meaning if your isMovingForward variable relies on this then it will never change. That is, if this script is on the client of course.

1 Like

Thanks for the response!
I’ve spent the past 2 hours trying out your code and yet the only result I could get was either one in which none of the animations turned on at all, or this:

I know that I’m probably doing something wrong, and so I’ll send a roblox file in a minute with just the flight script in it
This is a slightly updated version of the script which I forgot I had, however, the issue is the same

Thanks again!

flightscript.rbxl (23.9 KB)

EDIT: despite this being a newer script, the idle animation’s left leg is broken and tends to flap around a lot, it has since been fixed, so please ignore that