Animation plays when tool isn't even equipped

I’m working on this sprinting script in a tool where it plays an animation if shift is detected and stops with shift is lifted. It works fine, until I unequip the tool and it still plays even though the tool is not equipped. How can I fix this?

Code:

local Player = game.Players.LocalPlayer

local Character = Player.Character

local Humanoid = Character.Humanoid

local UIS = game:GetService('UserInputService')

local SprintingAnimation = Humanoid:LoadAnimation(script.Parent:WaitForChild("SprintingAnimation"))



UIS.InputBegan:connect(function(key, gameProcessed) 

	if key.KeyCode == Enum.KeyCode.LeftShift then
		
		script.Parent.Values:WaitForChild("IsSprinting").Value = true
		
		SprintingAnimation:Play()
		
		print("Sprinting")
		
	end
end)

UIS.InputEnded:connect(function(key, gameProcessed)

	if key.KeyCode == Enum.KeyCode.LeftShift then
		
		script.Parent.Values:WaitForChild("IsSprinting").Value = false
		
		SprintingAnimation:Stop()
		
		print("Stopped Sprinting")
		
		end
	end)

You need to check if the tool is equipped/parented to the player’s character.

if not (tool.Parent == Character) then
	return
end
1 Like

It doesn’t seem to work even when the tool is equipped

My bad. I used a script, instead of tool.

local tool = script.Parent
1 Like