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)
To determine if the tool is equipped or not, there are two events that belong to the Tool class.
The first being Tool.Equipped. This event fires when a tool is equipped by a player.
The second is Tool.Unequipped. This event fires when a tool is unequipped by a player.
A way you could incorporate these events into your code is:
-- 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 local IsEquipped = false local tool = -- location of tool -- 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 and IsEquipped 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) tool.Equipped:Connect(function() IsEquipped = true end) tool.Unequipped:Connect(function() IsEquipped = false sprintTrack:Stop(0) end)