Animation doesn't stop when unequipping!

Hello!

I’ve made a script that seems to work just fine for everything except when I unequip the tool the animation doesn’t stop. How can I go about fixing this issue.

LocalScript:

local Tool = script.Parent
local Animation = Tool.Animation
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent
local rounds = 8
local reloading = false

Tool.Equipped:Connect(function()
	local Character = Tool.Parent
	local Humanoid = Character.Humanoid
	
	local AnimationTrack = Humanoid:LoadAnimation(Animation)
	AnimationTrack:Play()
	player.PlayerGui.GunHud.Enabled = true
end)


tool.Unequipped:Connect(function()
	local Character = player.Character
	local Humanoid = Character.Humanoid

	local AnimationTrack = Humanoid:LoadAnimation(Animation)
	AnimationTrack:Stop()
	
	player.PlayerGui.GunHud.Enabled = false
end)

tool.Activated:Connect(function()
	if reloading == false and rounds >= 1 then
		tool.firesound:Play()
		if rounds >= 1 then
			wait(.1)
			rounds = rounds -1
		end 
		if mouse.Target.Parent then
			script.Parent.RemoteEvent:FireServer(mouse.Target.Parent)
		end
		player.PlayerGui.GunHud.Ammo.Text = rounds.."/8"
	end

	if rounds == 0 then
		reloading = true
		tool.reload:Play()
		wait(1.6)
		rounds = 8
		reloading = false
		player.PlayerGui.GunHud.Ammo.Text = rounds.."/8"
	end



end)

Thank you!

You are loading a new animation, and stoping that one new loaded animation, and not the other one

you have to create a variable and then when you load the animation on equip, you should set the variable to the loaded animation


local AnimationTrack

Tool.Equipped:Connect(function()
	local Character = Tool.Parent
	local Humanoid = Character.Humanoid
	
    AnimationTrack = Humanoid:LoadAnimation(Animation) -- Set the animation to what you want
	AnimationTrack:Play()
	player.PlayerGui.GunHud.Enabled = true
end)


tool.Unequipped:Connect(function()
	local Character = player.Character
	local Humanoid = Character.Humanoid

	AnimationTrack:Stop()
	
	player.PlayerGui.GunHud.Enabled = false
end)
1 Like

This worked for me. Thank you very much!

1 Like

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