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)
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)
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
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.