Animation won't work

So I recently modified an Community Fly Script
I don’t exactly remember where the Thread is but let’s start

So I studied a bit of the Code then Modified it when I modified it broke

Here is the Code:

local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local CAS = game:GetService("ContextActionService")

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local Camera = workspace.CurrentCamera

-- Events
local BOOMERANG_EVENT = game.ReplicatedStorage.Events.Boomerang
local TELEPORT_EVENT = game.ReplicatedStorage.Events.Teleport
local FLYING_EVENT = game.ReplicatedStorage.Events.Flying

local Debounce = false

-- requirements
local Character = Player.Character
local Humanoid = Character.Humanoid
local animate = Character:WaitForChild("Animate")
local HumanoidRootPart = Character.HumanoidRootPart
local counter = 0

local bodyGyro = Instance.new("BodyGyro");
bodyGyro.maxTorque = Vector3.new(1, 1, 1)*10^6;
bodyGyro.P = 10^6;

local bodyVel = Instance.new("BodyVelocity");
bodyVel.maxForce = Vector3.new(1, 1, 1)*10^6;
bodyVel.P = 10^4;

local isFlying = false;
local isJumping = false;
local movement = {forward = 0, backward = 0, right = 0, left = 0};

-- Animations
local idleAnim = Humanoid:LoadAnimation(script.Parent.Parent:WaitForChild("AnimationTracks").FlyAnims.FlyIdle)
local moveAnimForward = Humanoid:LoadAnimation(script.Parent.Parent:WaitForChild("AnimationTracks").FlyAnims.FlyForwards)
local moveAnimBackward = Humanoid:LoadAnimation(script.Parent.Parent:WaitForChild("AnimationTracks").FlyAnims.FlyBackwards)
local moveAnimRight = Humanoid:LoadAnimation(script.Parent.Parent:WaitForChild("AnimationTracks").FlyAnims.FlyRight)
local moveAnimLeft = Humanoid:LoadAnimation(script.Parent.Parent:WaitForChild("AnimationTracks").FlyAnims.FlyLeft)

local lastAnim = idleAnim;

local function setFlying(flying)
	isFlying = flying;
	bodyGyro.Parent = isFlying and HumanoidRootPart or nil;
	bodyVel.Parent = isFlying and HumanoidRootPart or nil;
	bodyGyro.CFrame = HumanoidRootPart.CFrame;
	bodyVel.Velocity = Vector3.new();
	
	animate.Disabled = isFlying;
	
	if (isFlying) then
		lastAnim = idleAnim;
		lastAnim:Play();
	else
		lastAnim:Stop();
	end
end

local function onUpdate(dt)
	if (isFlying) then
		local cf = Camera.CFrame;
		local direction = cf.rightVector*(movement.right - movement.left) + cf.lookVector*(movement.forward - movement.backward);
		
		if (direction:Dot(direction) > 0) then
			direction = direction.unit;
		end
		
		bodyGyro.CFrame = cf;
		bodyVel.Velocity = direction * Humanoid.WalkSpeed * 3;
	end
end

local function onJumpRequest()
	if (not Humanoid or Humanoid:GetState() == Enum.HumanoidStateType.Dead) then
		return;
	end
	
	if (isFlying) then
		setFlying(false);
		isJumping = false;
	elseif (isJumping) then
		setFlying(true);
	end
end

local function onStateChange(old, new)
	if (new == Enum.HumanoidStateType.Landed) then	
		isJumping = false;
	elseif (new == Enum.HumanoidStateType.Jumping) then
		isJumping = true;
	end
end

local function movementBind(actionName, inputState, inputObject)
	if (inputState == Enum.UserInputState.Begin) then
		movement[actionName] = 1;
	elseif (inputState == Enum.UserInputState.End) then
		movement[actionName] = 0;
	end
	
	if (isFlying) then
		local isMoving = movement.right + movement.left + movement.forward + movement.backward > 0;
		local nextAnim
		if movement.right > 0 then -- right move
			nextAnim = moveAnimRight
		elseif movement.left > 0 then
			nextAnim = moveAnimLeft
		elseif movement.backward > 0 then
			nextAnim = moveAnimBackward
		elseif movement.forward > 0 then
			nextAnim = moveAnimBackward
		end
		if (nextAnim ~= lastAnim) then
			lastAnim:Stop();
			lastAnim = nextAnim;
			lastAnim:Play();
		end
	end
	
	return Enum.ContextActionResult.Pass;
end

local function KeyComboForLightning()
	local KeysPressed = UIS:GetKeysPressed()
	
	local CTRL, L = false, false
	
	for _, Keys in pairs(KeysPressed) do
		if Keys.KeyCode == Enum.KeyCode.LeftControl then
			CTRL = true
		elseif Keys.KeyCode == Enum.KeyCode.L then
			L = true
		end
	end
	
	if CTRL and L then
		L = false
		CTRL = false
		
	end
end

UIS.InputBegan:Connect(function(input, IsTyping)
	if not IsTyping then
		if input.KeyCode == Enum.KeyCode.T then
			
		elseif input.KeyCode == Enum.KeyCode.V then
			
		elseif input.KeyCode == Enum.KeyCode.Space then
			counter = counter + 1
			
			if counter == 2 then
				onJumpRequest()
			elseif counter == 3 then
				counter = 0
				setFlying(false)
			end
		elseif input.KeyCode == Enum.KeyCode.L then
			
		end
	end
end)

Humanoid.StateChanged:Connect(onStateChange);
-- UIS.JumpRequest:Connect(onJumpRequest);

CAS:BindAction("forward", movementBind, false, Enum.PlayerActions.CharacterForward);
CAS:BindAction("backward", movementBind, false, Enum.PlayerActions.CharacterBackward);
CAS:BindAction("left", movementBind, false, Enum.PlayerActions.CharacterLeft);
CAS:BindAction("right", movementBind, false, Enum.PlayerActions.CharacterRight);

RS.RenderStepped:Connect(onUpdate)

Errors that are causing it

15:19:21.289 - Workspace.FerbZides.ClientScripts.UIS:119: attempt to index nil with 'Play'
15:19:21.291 - Stack Begin
15:19:21.296 - Script 'Workspace.FerbZides.ClientScripts.UIS', Line 119 - function movementBind
15:19:21.297 - Stack End
15:19:21.298 - ContextActionService: Unexpected error while invoking callback: Workspace.FerbZides.ClientScripts.UIS:119: attempt to index nil with 'Play'

PS:
Move Direction of Humanoid may help but I don’t know how to do it whatsoever

Are you checking that the animation is loaded yet?

Remember indexing after a wait for child statement is not safe because of the top down model roblox operates in.

1 Like
-- Animations
local idleAnim = Humanoid:LoadAnimation(script.Parent.Parent:WaitForChild("AnimationTracks").FlyAnims.FlyIdle)
local moveAnimForward = Humanoid:LoadAnimation(script.Parent.Parent:WaitForChild("AnimationTracks").FlyAnims.FlyForwards)
local moveAnimBackward = Humanoid:LoadAnimation(script.Parent.Parent:WaitForChild("AnimationTracks").FlyAnims.FlyBackwards)
local moveAnimRight = Humanoid:LoadAnimation(script.Parent.Parent:WaitForChild("AnimationTracks").FlyAnims.FlyRight)
local moveAnimLeft = Humanoid:LoadAnimation(script.Parent.Parent:WaitForChild("AnimationTracks").FlyAnims.FlyLeft)

pretty much waiting for the parent to load

Did you read the post I sent you?

Edit, also it’s always a good idea to have a nil check before executing the code in order to avoid the nil index error.

1 Like

what kind of nil check? and this is the path parent>parent>object

In an if statement we can use the variable leading to the object itself to see if the object exists or not from the roblox API reference

Note that Lua considers the “absence of true” as “false.” This means that both false and nil will evaluate as non-true

This means you can do this to your code to check is lastAnim exists before you :Play() on lastAnim.

		if  lastAnim and (nextAnim ~= lastAnim) then
			lastAnim:Stop();
			lastAnim = nextAnim;
			lastAnim:Play();
		end

1 Like

nevermind I fixed it by checking if the Player was idle and not moving with another check of it