Ok, so I'm making a gun that when you press down the right button a animation plays and moves your gun closer to you for better aim. It works, but when I unequipped the gun, the animation plays when I right click. I have this script so it checks that you are equipped to the gun before you can play the gun (Line 3) I really need help!! Anything helps! Here is the script:
local mouse = game.Players.LocalPlayer:GetMouse() tool = script.Parent.Parent if tool.Activated then tool.Equipped:connect(function() game.Players.LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson mouse.Button2Down:Connect(function() local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ZoomInAnimation) animation:Play() end) end) mouse.Button2Up:Connect(function() local Animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ZoomOutAnimation) Animation:Play() if tool.Deactivated then tool.Unequipped:Connect(function(mouse) game.Players.LocalPlayer.CameraMode = Enum.CameraMode.Classic local player = game.Players.LocalPlayer function StopAnimations() local ActivTracks = player.Character.Humanoid:GetPlayingAnimationTracks() for _, v in pairs(ActivTracks) do v:Stop() end end end) end
i did some testing in studio and came up with this and it works like you want to
local mouse = game.Players.LocalPlayer:GetMouse() tool = script.Parent.Parent mouse.Button2Down:Connect(function() tool.Equipped:connect(function() game.Players.LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ZoomInAnimation) animation:Play() end) end) mouse.Button2Up:Connect(function() tool.Equipped:connect(function() game.Players.LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson local Animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.ZoomOutAnimation) Animation:Play() end) end) tool.Unequipped:Connect(function(mouse) game.Players.LocalPlayer.CameraMode = Enum.CameraMode.Classic local player = game.Players.LocalPlayer local ActivTracks = player.Character.Humanoid:GetPlayingAnimationTracks() for _, v in pairs(ActivTracks) do v:Stop() end end)
this checks in a better way if you click down and if the tool is eqipped, if both are true then it plays the animation