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