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

I'm making a gun that when you press down the right button it moves the gun closer?

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

1 comment

0

tool.Activated only happens after tool.Equipped so remove line 3

1 answer

✓ Accepted answer

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

5 comments

0

Thanks for answering I will try this out.

0

Sadly it seems even after I unequip the weapon, when I press right click down, the animation still plays :C I think the issue is somehow I need to check if the tool is equipped before doing anything.

0

works now

0

Ok, I'll try it thanks!

1

Thank you SOO much!! I've been trying all last night trying to get it to work!!! You are AWESOMEE!!!

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