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

Animation isn't stopping after tool is unequipped?

I'm trying to stop an animation after the tool gets unequipped but the animation still sticks around being a burden on the regular walking animation. I'm not sure what to do.

local Gun = script.Parent
local Handle = Gun:WaitForChild("Handle")
local Event = Gun: WaitForChild("RemoteEvent")
local Swing = Instance.new("Animation",script.Parent)
local Equip = Instance.new("Animation", script.Parent)
local CanFire = true
Swing.AnimationId = "http://www.roblox.com/Asset?ID=700778123"
Equip.AnimationId = "http://www.roblox.com/Asset?ID=700657273"

FireRate = 1

local trackanimation = nil
local trackanimation2 = nil

function playAnimation(Anim)
	local plr = game.Players.LocalPlayer
	trackanimation = plr.Character.Humanoid:LoadAnimation(Anim)
	trackanimation.KeyframeReached:Connect(function(kf)print("Working")end)
	trackanimation:Play()
end

function playAnimation2(Anim)
	local plr = game.Players.LocalPlayer
	trackanimation2 = plr.Character.Humanoid:LoadAnimation(Anim)
	trackanimation2.KeyframeReached:Connect(function(kf)print("Working")end)
	trackanimation2:Play()
end


Gun.Equipped:Connect(function(Mouse)
	if Mouse then
		playAnimation(Equip) --Play the equip animation
		Mouse.Button1Down:Connect(function()
			playAnimation2(Swing) --Play the animation for swining
			if CanFire then
				CanFire=false
				playAnimation(Swing)
				print("Firing")
				Event:FireServer("Fire", Mouse.Target)
				wait(FireRate)
				CanFire=true
			end
		end)
	end
end)

Gun.Unequipped:Connect(function(Mouse)
	local plr = game.Players.LocalPlayer
	plr.Character.Humanoid:LoadAnimation(Equip):Stop()
	plr.Character.Humanoid:LoadAnimation(Swing):Stop()
end)

Here is a link of it happening. I apologize if this script is a mess, I just learned animating a few hours ago.

2 answers

Wipe the whole animation

Gun.Unequipped:Connect(function(Mouse)
    local plr = game.Players.LocalPlayer
    local animEquip = plr.Character.Humanoid:LoadAnimation(Equip)
    animEquip:Stop()
    animEquip:Destroy()
    animEquip = nil
    local animSwing = plr.Character.Humanoid:LoadAnimation(Swing)
    animSwing:Stop()
    animSwing:Destroy()
    animSwing = nil
end)

1 comment

0

Still does the same thing as before.

You'll need to put the Unequipped() function call inside of the Equipped(), like this:

Gun.Equipped:Connect(function()
	if Mouse then
		Equip:Play()
		Gun.Activated:Connect(function()
			if CanFire then
				CanFire = false
				Swing:Play()
				print("Firing")
				Event:FireServer("Fire", Mouse.Target)
				wait(FireRate)
				CanFire = true
			end
		end)
	end
	Gun.Unequipped:Connect(function()
		Equip:Stop()
	end)
end)

This should work.

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