AnimationTrack:Stop() doesn't work

So I made a script that makes the player play an animation when he moves left.
The problem is that when I play the animation, it works properly but when I tried to stop it, it says its an unknown global.

Here is the script :

local function onLeft(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		local animation = Instance.new("Animation")
		local animationId = 10874895922
		animation.AnimationId = "rbxassetid://" ..animationId
		local AnimationTrack = hum:LoadAnimation(animation)
		AnimationTrack:Play(0.100000001, 1, -1)
		leftValue = 1
		currentDirection = -1
		playerOrient(math.rad(90))
		lastDirection = -1
	elseif inputState == Enum.UserInputState.End then
		Animationtrack:Stop()
		leftValue = 0
	end
end

Remove the local in local AnimationTrack.

1 Like

What is happening is that you have something local inside a different if statement so what you could is you can set it before the if statement like
local AnimTrack
then just remove local for all the other AnimTrack variables

1 Like
local AnimationTrack

local function onLeft(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		if not AnimationTrack then --Check if animation is loaded.
			local animation = Instance.new("Animation")
			local animationId = 10874895922
			animation.AnimationId = "rbxassetid://" ..animationId
			AnimationTrack = hum:LoadAnimation(animation) --Load animation if not loaded.
		end
		AnimationTrack:Play(0.100000001, 1, -1) --Play loaded animation.
		leftValue = 1
		currentDirection = -1
		playerOrient(math.rad(90))
		lastDirection = -1
	elseif inputState == Enum.UserInputState.End then
		AnimationTrack:Stop()
		leftValue = 0
	end
end

Just to clarify what the other replies were inferring.

1 Like