Sprint won't stop after shift key released

local uis = game:GetService("UserInputService")

local tool = script.Parent

local armed = false
local running = false

local runningAnim
local armedAnim
local char

tool.Equipped:Connect(function()
	armed = true
	char = tool.Parent
	armedAnim = char.Humanoid:LoadAnimation(script.Parent.Animations.Armed)
	armedAnim:Play()
	armedAnim.Looped = true
end)

tool.Unequipped:Connect(function()
	armed = false
	if armedAnim ~= nil then
		armedAnim.Looped = false
		armedAnim:Stop()
		armedAnim = nil
		char = nil
	end
end)

uis.InputBegan:Connect(function(input,gameProcessed)
	if not gameProcessed then
		if armed == true then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local key = input.KeyCode
				if key == Enum.KeyCode.LeftShift then
					running = true
					armedAnim:Stop()
					armedAnim.Looped = false
					runningAnim = char.Humanoid:LoadAnimation(script.Parent.Animations.Sprint)
					runningAnim:Play()
					runningAnim.Looped = true
				end
			end
		end

	end
end)

uis.InputBegan:Connect(function(input,gameProcessed)
	if not gameProcessed then
		if armed == true then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local key = input.KeyCode
				if key == Enum.KeyCode.LeftShift then
					running = false
					runningAnim:Stop()
					runningAnim.Looped = false
					armedAnim:Play()
					armedAnim.Looped = true
					runningAnim = nil
				end
			end
		end

	end
end)

Players.vf9r.Backpack.Rifle.LocalScript:56: attempt to index nil with 'Looped' - Client - LocalScript:56

What did I do wrong here? It works perfectly fine with the equip and unequip, even with the sprint, but when I stop sprinting, it just errors what I mentioned above but doesn’t stop the animation.

1 Like

I looks like the animation is nil for some reason.

When you equip the tool and start sprinting, does it run the animation?
Also, if you equip the tool start sprinting and then stop sprinting, does it stop the animation?

What happens if you equip the tool, start sprinting, unequipped the tool and then release shift?

Look at the script carefully, you’re doing InputBegan two times. Make the last event InputEnded.

1 Like

Why did you remove your comment? I believe you were correct.
@vf9r you do use InputBegan twice, you should change the second one to InputEnded.

The error was something else, so i got a little confused.

The error is something else, but he should actually also use an InputEnded.

I think you shouldn’t set the animations to nil every time you stop them.
Make them variables that will always be assigned to the animations, and simply :Stop() or :Play() them when needed.