Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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
0
tool.Activated only happens after tool.Equipped so remove line 3 Gameplayer365247v2 1055 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
Thanks for answering I will try this out. Mrmonkeyman120 65 — 4y
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. Mrmonkeyman120 65 — 4y
0
works now Gameplayer365247v2 1055 — 4y
0
Ok, I'll try it thanks! Mrmonkeyman120 65 — 4y
1
Thank you SOO much!! I've been trying all last night trying to get it to work!!! You are AWESOMEE!!! Mrmonkeyman120 65 — 4y
Ad

Answer this question