Hello! I've been trying for a bit to get this dang issue from happening. Here is the script I am using, this is in StarterCharacterScripts
. Also, this is a Local Script, and I have two Remote Events in ReplicatedStorage
, and two Server Scripts in ServerScriptService
. Hope you can figure out whats going on! -Mrmonkeyman120
local tool = game.StarterPack.M4 local db = true local UserInputService = game:GetService('UserInputService') local anim = Instance.new("Animation") anim.AnimationId = 'https://www.roblox.com/asset/?id=3345626311' local playAnim = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(anim) game.ReplicatedStorage.ShiftToSprintHoldGun:FireServer() UserInputService.InputBegan:Connect(function(input, event) if input.KeyCode == Enum.KeyCode.LeftShift and db then db = false if tool.Equipped then playAnim:Play() db = true end end end) if tool.Unequipped then local Anim = Instance.new("Animation") Anim.AnimationId = 'https://www.roblox.com/asset/?id=3345452716' local playanim = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(Anim) game.ReplicatedStorage.ShiftToSprintHoldGun:FireServer() UserInputService.InputBegan:Connect(function(input, Event) if input.KeyCode == Enum.KeyCode.LeftShift and db then db = false playAnim:Stop() wait(0.15) playanim:Play() print("Worked") end end) end
The tool will be in the player's character or backpack, if it's equipped it's in the character. Which means that you should check both the backpack and character.
local tool = game.Players.LocalPlayer.Backpack:FindFirstChild("M4") or game.Players.LocalPlayer.Character:FindFirstChild("M4")
Your problem is on line 17
if tool.Unequipped then
If you want detect when tool is unequipped, you will need function.
tool.Unequipped:Connect(function() end)
and line 11 too
tool.Equipped:Connect(function() end)
also change StarterPack to player backpack
Here is the script
local tool = game.Players.LocalPlayer.Backpack:FindFirstChild("M4") or game.Players.LocalPlayer.Character.Backpack:FindFirstChild("M4") local db = true local UserInputService = game:GetService('UserInputService') local anim = Instance.new("Animation") anim.AnimationId = 'https://www.roblox.com/asset/?id=3345626311' local playAnim = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(anim) game.ReplicatedStorage.ShiftToSprintHoldGun:FireServer() local key = nil UserInputService.InputBegan:Connect(function(input, event) if input.KeyCode == Enum.KeyCode.LeftShift and db then db = false key = input.KeyCode end end) tool.Equipped:Connect(function() if key == Enum.KeyCode.LeftShift and db then playAnim:Play() db = true end end) tool.Unequipped:Connect(function() local Anim = Instance.new("Animation") Anim.AnimationId = 'https://www.roblox.com/asset/?id=3345452716' local playanim = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(Anim) game.ReplicatedStorage.ShiftToSprintHoldGun:FireServer() UserInputService.InputBegan:Connect(function(input, Event) if input.KeyCode == Enum.KeyCode.LeftShift and db then db = false playAnim:Stop() wait(0.15) playanim:Play() print("Worked") end end) end)