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

Why won't my animation stop after I unequip the tool?

I just want to say sorry for the late replies because my internet keeps going on and off but, I have this script which plays an idle animation for a tool whenever the player is standing still with it equipped. Yes that part does work but the only thing is the animation does not stop whenever the tool is unequipped. It is as if the Running function ignored everything and won't stop when the tool is unequipped. (btw I did try ending the function sooner)

local Player = script.Parent.Parent.Parent
local Tool = script.Parent
local weld = Instance.new("Weld", Tool)
weld.Part0 = Tool.Blade
weld.Part1 = Player.Character:WaitForChild("GripBrick")
weld.C1 = CFrame.new(0, 0, -1) * CFrame.Angles(math.rad(0), math.rad(90), math.rad(180))

--Sheathe-------------------------------------------------------------

local Sheathe = Tool.Sheathe
Sheathe.Parent = Player.Character
local weld = Instance.new("Weld",Sheathe)
weld.Part1 = Sheathe.HumanoidRootPart
weld.Part0 = Player.Character.Torso
weld.C0 = CFrame.new(0,-.25,0)


-------------------------------------------------------------------------
local Track1 = Instance.new("Animation")
Track1.AnimationId = "rbxassetid://6074005467"
local Equip = Player.Character.Humanoid:LoadAnimation(Track1)

local Track2 = Instance.new("Animation")
Track2.AnimationId = "rbxassetid://6074005467"
local Unequip = Player.Character.Humanoid:LoadAnimation(Track2)

local Track3 = Instance.new("Animation")
Track3.AnimationId = "rbxassetid://6074686611"
local Idle = Player.Character.Humanoid:LoadAnimation(Track3)

-------------------------------------------------------------------------

Tool.Equipped:Connect(function()
	Equip:Play()
	
	Player.Character.Humanoid.Running:connect(function()
		wait(1)
		if Player.Character.Torso.Velocity.Magnitude < 0.2 then
			Idle:Play()
		elseif Player.Character.Torso.Velocity.Magnitude > 0.2 then 
			Idle:Stop() 
		end
--I tried adding the end) here, same issues

	Sheathe.Blade.Transparency = 1
	local b = Instance.new("BoolValue")
	b.Parent  = Player.Character
	b.Name = "Equipped"
	local bs = Instance.new("BoolValue")
	bs.Parent  = Player.Character
	bs.Name = "Hit"
	game.Debris:AddItem(bs,1)
	Sheathe.Blade.Sheathe:Play()
	Sheathe.Blade.Sheathe:Stop()
end)


Tool.Unequipped:Connect(function()
	Unequip:Play()
	Idle:Stop() --stop that did not work
	Sheathe.Blade.Unsheathe:Play()
	wait(0.5)
	Sheathe.Blade.Transparency = 0
	if Player.Character:FindFirstChild("Equipped") then 
	Player.Character.Equipped:Destroy()
	end
	Sheathe.Blade.Unsheathe:Stop()
end)
end)

1 answer

✓ Accepted answer

Ends are in the wrong spot for one, but that isn't your main problem. You connected the running function and never disconnected it, so it will just keep replaying.

local Player = script.Parent.Parent.Parent
local Tool = script.Parent
local weld = Instance.new("Weld", Tool)
weld.Part0 = Tool.Blade
weld.Part1 = Player.Character:WaitForChild("GripBrick")
weld.C1 = CFrame.new(0, 0, -1) * CFrame.Angles(math.rad(0), math.rad(90), math.rad(180))

--Sheathe-------------------------------------------------------------

local Sheathe = Tool.Sheathe
Sheathe.Parent = Player.Character
local weld = Instance.new("Weld",Sheathe)
weld.Part1 = Sheathe.HumanoidRootPart
weld.Part0 = Player.Character.Torso
weld.C0 = CFrame.new(0,-.25,0)


-------------------------------------------------------------------------
local Track1 = Instance.new("Animation")
Track1.AnimationId = "rbxassetid://6074005467"
local Equip = Player.Character.Humanoid:LoadAnimation(Track1)

local Track2 = Instance.new("Animation")
Track2.AnimationId = "rbxassetid://6074005467"
local Unequip = Player.Character.Humanoid:LoadAnimation(Track2)

local Track3 = Instance.new("Animation")
Track3.AnimationId = "rbxassetid://6074686611"
local Idle = Player.Character.Humanoid:LoadAnimation(Track3)
local isrunning -- define variable to connect humanoid:Running to
-------------------------------------------------------------------------

Tool.Equipped:Connect(function()
	Equip:Play()
	if isrunning == nil then -- Lets make sure we dont connect twice
		isrunning = Player.Character.Humanoid.Running:Connect(function() -- First of all when you Connect a function, it stays connected until the code finishes, which it wont here, or you disconnect it yourself. This is why we define the connection as a variable. Secondly, connect is depreciated, use Connect.
			wait(1)
			if Player.Character.Torso.Velocity.Magnitude < 0.2 then
				Idle:Play()
			elseif Player.Character.Torso.Velocity.Magnitude > 0.2 then 
				Idle:Stop() 
			end
			--I tried adding the end) here, same issues

			Sheathe.Blade.Transparency = 1
			local b = Instance.new("BoolValue")
			b.Parent  = Player.Character
			b.Name = "Equipped"
			local bs = Instance.new("BoolValue")
			bs.Parent  = Player.Character
			bs.Name = "Hit"
			game.Debris:AddItem(bs,1)
			Sheathe.Blade.Sheathe:Play()
			Sheathe.Blade.Sheathe:Stop()
		end)
	end
end) -- You were connecting the Unequip function within the Equip function, so every time you equipped the tool, another Unequip function would connect, resulting in multiple instances.
Tool.Unequipped:Connect(function()
	if isrunning ~= nil then
		isrunning:Disconnect() -- Disconnect previous function.
	end
		Unequip:Play()
		Idle:Stop() --stop that did not work
		Sheathe.Blade.Unsheathe:Play()
		wait(0.5)
		Sheathe.Blade.Transparency = 0
		if Player.Character:FindFirstChild("Equipped") then 
			Player.Character.Equipped:Destroy()
		end
		Sheathe.Blade.Unsheathe:Stop()
	end)

--Basically what was happening was your Humanoid:IsRunning function stayed connected, so it would just keep replaying the anim. Now it will disconnect on unequip.

4 comments

0

Oh I see my problems now, thank you for the help. I didn't really did that much research on disconnecting functions

0

Actually I am having one more problem now, the whole thing completely stops, what can I do about that? Like whenever I unequip the tool the idle won't play again when I re-equip it

0

Im sorry I forgot when you disconnect an event it doesnt set that variable to nil, so the script wont refire. Simply type isrunning = nil underneath isrunning:Disconnect()

0

Alright thank you

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