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)