So I have a local script in a tool for a gun and it plays this holding gun position for the sprint, but when I have the tool in my inventory, when I press shift key, it plays the holding gun animation even when its holding nothing mid air. How do I make the animation only play when the tool is equipped? I tried fixing but it was not use. Hope someone could help this. Gladly appreciate it!
-- Services
local userInputService = game:GetService("UserInputService")
local playersService = game:GetService("Players")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local sp = script.Parent
-- Variables
local player = playersService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local pcButton = Enum.KeyCode.LeftShift
local xboxButton = Enum.KeyCode.ButtonL3
-- Animation
local animation = script:WaitForChild("SprintAnim")
local sprintTrack = humanoid:LoadAnimation(animation)
-- Input controller
userInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == (pcButton or xboxButton) and not processed then
sprintTrack:Play(.1)
end
end)
userInputService.InputEnded:Connect(function(input, processed)
if input.KeyCode == (pcButton or xboxButton) and not processed then
sprintTrack:Stop(.1)
end
end)
Character.ChildRemoved:Connect(function(RemovedChild)
if RemovedChild:IsA("Tool") then
sprintTrack:Stop(0)
--the tool is unequipped
--RemovedChild is the tool unequipped
end
end)

4 comments
I tried this script, but it still plays the animation when the tool isn't equipped. On line 18 I putted script.Parent becuase the local script is parented in the tool. Not sure if I did it right but the issue is still there.
Updated my answer. There was one line of code I meant to add but forgot to.
Thank you so much! But was the error you did? I just want to know and get used to coding.
I added a check for "IsEquipped" to line 26