Mouse Input still gets detect even if tool is not equipped

So I made a tool animation script but I noticed that even if I want to hold the tool and click the mouse button it would run the mouse input function. And I would get this error:

Players.ForgottenDogYT.Backpack.TestAK.Animations:44: attempt to index nil with 'Stop'

This is the tool animation script. The MouseDetection is down so scroll down to see it:

--PlayerStuff

local player = game.Players.LocalPlayer

local Mouse = player:GetMouse()

local tool = script.Parent

local Reloading = tool.Handle.ReloadStuff.Reloading

local Ammo = tool.Handle.ReloadStuff.AmmoValue

--Shoot Anim

local ShootAnim = Instance.new("Animation")

ShootAnim.AnimationId = "rbxassetid://7039142777"

local track2

--Hold Anim

local HoldAnim = Instance.new("Animation")

HoldAnim.AnimationId = "rbxassetid://7039052953"

local track

--Tool Functions

tool.Equipped:Connect(function()

track = script.Parent.Parent.Humanoid:LoadAnimation(HoldAnim)

track.Priority = Enum.AnimationPriority.Action

track.Looped = true

track:Play()

end)

tool.Unequipped:Connect(function()

if track then

track:Stop()

Mouse:Disconnect()

end

end)

--Fire

Mouse.Button1Down:Connect(function()

if Reloading.Value == false and Ammo.Value ~= 0 then

track2 = script.Parent.Parent.Humanoid:LoadAnimation(ShootAnim)

track2.Priority = Enum.AnimationPriority.Action

track2.Looped = true

track2:Play()

elseif Reloading.Value == true then

track2:Stop()

end

end)

Mouse.Button1Up:Connect(function()

track2:Stop()

end)

Reloading.Changed:Connect(function()

if Reloading.Value == true then

track2:Stop()

end

end)

Ammo.Changed:Connect(function()

if Ammo.Value == 0 then

track2:Stop()

end

end)

using Activated event, It will fire on mouse button 1 click(pc), touch(mobilde).

yeah but my guns are full auto so i need to detect if the mouse is being held/

The Tool.Equipped event, has a parameter which is the mouse. You can pass in the mouse through that parameter, and connect it with your Mouse.Button1Down and Mouse.Button1Up Events.

tool.Equipped:Connect(function(mouse)
    -- Connect mouse with your events
end)

Also, Humanoid:LoadAnimation is deprecated, so you should be using Animator:LoadAnimation instead. An example of how you would use it here is:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")

if Humanoid then 
	local Animator = Humanoid:FindFirstChildOfClass("Animator")  -- Get the Animator
	if Animator then
		track = Animator:LoadAnimation(HoldAnim) -- Load the animation
		track2 = Animator:LoadAnimation(ShootAnim)
	end
end

For the mouse function? How would I make it work?

Can you clarify what you mean? You simply connect the mouse parameter in the equipped event to your mouse events.

tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		-- do whatever
	end)
	
	mouse.Button1Up:Connect(function()
		-- -- do whatever
	end)
end)

Oh that’s what I meant! An example. My bad. Thanks for the example. Also do I remove local Mouse = player:GetMouse

It depends if you need the mouse outside of the Equipped event. For example hooking the Mouse up with some event or whatever outside of your tool.Equipped event.

1 Like

Activated event will fire on button1down, Deactivated event fire on button1up

tool.Activated:connect(function()
--something start
end)
tool.Deactivated:connect(function()
--something stop!
end)

don’t do that, you will create more and more event Button1Down and Button1Up
ex:

tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		print'something'
	end)
end)

something will be printed equal equiped times