SCRIPTING How do i stop animation in gun tool after playing

So i am making a gun and i want it to play an animation when it aims the gun but i dont know how to make it stop the animation when it stops aiming, the line i am trying to put something on is in the second lua panel in caps lock plz help the first lua panel is the animation play function

local playanimation = remotes:WaitForChild("PlayAnimation")



playAnimation.OnServerEvent:Connect(function(player, animation, speed)
	local animation = player.Character:WaitForChild("Humanoid"):LoadAnimation(animation)
	animation:Play()
	animation:AdjustSpeed(speed)
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProccesed)
		if not gameProccesed and input.UserInputType == Enum.UserInputType.MouseButton2 or input.KeyCode == Enum.KeyCode.LeftControl then
			local firstPerson = isFirstPerson()
			
		
			if firstPerson and config:WaitForChild("Ironsight").Value then
				playanimation:FireServer(tool:WaitForChild("Animations"):WaitForChild("Aim"), tool:WaitForChild("Animations"):WaitForChild("Aim"):WaitForChild("Speed").Value)
				
				tween1:Play()
				if config:WaitForChild("IronsightWalkSpeedReduce").Value then
					humanoid.WalkSpeed = config:WaitForChild("IronsightWalkSpeed").Value
				end
			end
		end
	end)
	
	UserInputService.InputEnded:Connect(function(input, gameProccesed)
		if not gameProccesed or input.UserInputType == Enum.UserInputType.MouseButton2 or input.KeyCode == Enum.KeyCode.LeftControl then
			tween2:Play()
			--THE CODE THAT STOPS THE ANIMATION GOES HERE
			if config:WaitForChild("IronsightWalkSpeedReduce").Value then
				humanoid.WalkSpeed = game:GetService("StarterPlayer").CharacterWalkSpeed
				
				
			end
			
		end
	end)

Just do

if config:WaitForChild("IronsightWalkSpeedReduce").Value then
	humanoid.WalkSpeed = game:GetService("StarterPlayer").CharacterWalkSpeed
	animation:Stop()				
end

`

Attempt to index nil with stop

If i do local animation = tool:WaitForChild(“Animations”):WaitForChild(“Aim”) then it says stop is not a valid member of animation

local Humanoid = plr.Character.Humanoid
local ActiveTracks = Humanoid:GetPlayingAnimationTracks()
for _, v in pairs(ActiveTracks) do
	v:Stop()
end

Here we go, you can print out what is running then use a tostring() to lock on.

local Humanoid, str = plr.Character.Humanoid, nil
local ActiveTracks = Humanoid:GetPlayingAnimationTracks()	
for _, v in pairs(ActiveTracks) do str = tostring(v)
	if str == "walk" or str == "run" then
		v:Stop()	
	end
end

i did it already but thanks and this wasnt going to work since theres a hold animation

I recommend handling the animation client-side, perhaps in StarterCharacterScripts, so its faster and better (it will automatically replicate) and making the event a bindable event. Then you can do this:

local playanimation = bindables:WaitForChild("PlayAnimation")
local stopanimation = bindables:WaitForChild('StopAnimation')

local player = game.Players.LocalPlayer
local animation = player.Character:WaitForChild("Humanoid"):LoadAnimation(animation)
--[[You should rlly be using:
local animation = player.Character:WaitForChild("Humanoid").Animator:LoadAnimation(animation)
]]

--As it is client sided now, you can remove 'player' from these parameters
playanimation.Event:Connect(function(animation, speed)
	animation:Play()
	animation:AdjustSpeed(speed)
end)

stopanimation.Event:Connect(function(animation, speed)
	animation:Stop()
end)

if u want it server side, you can probably figure out how to store everyone’s animations in a table or something.

:Stop() is a method of an AnimationTrack instance, returned when you use Animator:LoadAnimation() or Humanoid:LoadAnimation() (you should really be using it on the animator as using it on the humanoid is deprecated as far as i know. Players should automatically have an Animator inside their Humanoid, if not then you can add one yourself). That’s why it errors when you call it on the Animation instance directly. It is done this way so the same Animation can be used in different humanoids using different AnimationTracks

Ya that works but, it sets off other problems … Not totally sure how to do this.
I added an edit to the org post that will work …

samarium , can you check this out , i dont know what to do I dont know how to stop function

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.