Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to fix shift key animation only when tool is equipped?

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)

1 answer

✓ Accepted answer

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)

4 comments

1

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.

1

Updated my answer. There was one line of code I meant to add but forgot to.

1

Thank you so much! But was the error you did? I just want to know and get used to coding.

1

I added a check for "IsEquipped" to line 26

This discussion is preserved as it was. New answers, comments, and votes are closed.