Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

I want to make a gun that zooms out and in. Why won't the animation stop when I unequip the gun?

Asked by 2 months ago

So I want to make a Animation play when you are equipped a gun and it plays an animation for your character to make the gun zoom in closer to get better aim. Everything works fine, but when you unequip the gun the animation just keeps playing? I tried many things like:

local anim = game.Players.LocalPlayer.Character.Humanoid(script.Parent.ZoomOutAnimation)
anim:Stop()
end

But for some reason it stil doesn't work.. Here is the LocalScript that is inside of the Handle of the gun. The two animation names are..ZoomInAnimation, and ZoomOutAnimation

local mouse = game.Players.LocalPlayer:GetMouse()
tool = script.Parent.Parent
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()
tool.Unequipped:Connect(function(mouse)
    game.Players.LocalPlayer.CameraMode = Enum.CameraMode.Classic
    local anim = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(ZoomInAnimation)
    anim:Stop()

Hopefully you can help! I thought this is pretty easy, but I have no idea why nothing is working!! Anything helps ;) -Mrmonkeyman120

0
@Mrmonkeyman120 cus u didnt tell it to stop the right wae TheluaBanana 868 — 2mo
0
Can you elaborate how I actually tell it to stop the RIGHT way? -Thanks! Mrmonkeyman120 43 — 2mo
0
make sure that you have some sort of way to tell if the item is in the players inventory/equipped before playing the animation capinbuilda 15 — 2mo
0
Yeah, I thought I need something like that. Mabye I could use ""if animation = Equiped then"? I'm not sure.. Mrmonkeyman120 43 — 2mo

2 answers

Log in to vote
0
Answered by 2 months ago

when you use local variable to load an animation, you are creating a new animation. then you stop that new animation. it has nothing to do the animation currently playing. you need to make variable outside your function, that all function are referring to the same animation you created only once.

or, you can just stop everything,

function StopAnimations()
    local ActivTracks = player.Character.Humanoid:GetPlayingAnimationTracks()
    for _, v in pairs(ActivTracks) do
        v:Stop()
    end

end

0
Ok I'ma test it Mrmonkeyman120 43 — 2mo
Ad
Log in to vote
0
Answered by 2 months ago
local mouse = game.Players.LocalPlayer:GetMouse()
tool = script.Parent.Parent
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()
tool.Unequipped:Connect(function(mouse)
    game.Players.LocalPlayer.CameraMode = Enum.CameraMode.Classic
function StopAnimations()
    local ActivTracks = player.Character.Humanoid:GetPlayingAnimationTracks()
    for _, v in pairs(ActivTracks) do
        v:Stop()
    end
end
end)
end)

That is what I have now, but it turns out when I unequip the weapon when I press MouseButton2 the animation still plays, I'm thinking its in the script because I have two red line under "StopAnimations()" and "player". But I think you have the right idea.

0
you cannot do this. this is what you need to fix. 1. remove line 15 and 20. 2. change player to game.Players.LocalPlayer as in the rest of your script TomAndelson 243 — 2mo

Answer this question