Help with stopping animation

Hey devforum I have a new problem, as my custom tool animation won’t stop playing for some unknown reason, for me that is. Can you please explain me how to fix this and the solution? It’s a modified script from a tutorial I watched.

local rs = game:GetService("ReplicatedStorage")
local ps = game:GetService("Players")

local events = rs.events
local models = rs.models
local animations = rs.animations
local animWeapons = animations.weapons
local weapons = models.weapons
local weaponsWeld = script.WeaponsWeld

local equipEvent = events:WaitForChild("equipEvent")

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local torso = char.Torso
		local valuesFolder = char:WaitForChild("valuesFolder")
		
		if valuesFolder then
			valuesFolder.Equipped.Value = false
			local weapon = weapons[valuesFolder.Weapon.Value]:Clone()
			
			weapon.Parent = char
			
			local weld = Instance.new("Motor6D")
			weld.Parent = char
			weld.Name = "weaponWeld"
			
			weld.Part0 = torso
			weld.Part1 = weapon
			
			weld.C1 = weaponsWeld[valuesFolder.Weapon.Value].weaponRest.C1
		end
	end)
end)

equipEvent.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local animator = char.Humanoid.Animator
	local torso = char.Torso
	local rightArm = char["Right Arm"]
	
	local weld = char.weaponWeld
	local valuesFolder = char:FindFirstChild("valuesFolder")
	
	local equipAnim = animator:LoadAnimation(animWeapons[valuesFolder.Weapon.Value].Equip)
	local holdAnim = animator:LoadAnimation(animWeapons[valuesFolder.Weapon.Value].Hold)
	local unequipAnim = animator:LoadAnimation(animWeapons[valuesFolder.Weapon.Value].Unequip)

	if valuesFolder and animator then
		if valuesFolder.Equipped.Value == false then
			equipAnim:Play()

			equipAnim:GetMarkerReachedSignal("Weld"):Connect(function()
				weld.Part0 = rightArm
				weld.C1 = weaponsWeld[valuesFolder.Weapon.Value].weaponHold.C1
			end)

			equipAnim:GetMarkerReachedSignal("Equipped"):Connect(function()
				valuesFolder.Equipped.Value = true
				holdAnim:Play()
			end)
		elseif valuesFolder.Equipped.Value == true then
			holdAnim:Stop()
			unequipAnim:Play()

			unequipAnim:GetMarkerReachedSignal("Weld"):Connect(function()
				weld.Part0 = torso
				weld.C1 = weaponsWeld[valuesFolder.Weapon.Value].weaponRest.C1
			end)

			unequipAnim:GetMarkerReachedSignal("Unequipped"):Connect(function()
				valuesFolder.Equipped.Value = false
			end)
		end
	end
end)

That’s because the event is creating a new animation track every time the event fires. You need to look for the animation track within the players animator if you want the same anim track

1 Like

Thank you so much Sonic! :grinning: I appreciate you helping me.

The method I used is to load the animation at the start of the script:

local PlayAnimation = hum:LoadAnimation(script:FindFirstChild("Animation"))

PlayAnimation:Play()
task.wait(1)
PlayAnimation:Stop()

Also, now that I get a better look at your script, you should have the animations be playing in a Local Script, as it’s more consistent and is better with ping. You could add a localscript into the tool you’re equipping and just play it there.

I’m not using a tool, but I like the idea of playing it in the client which I’m trying to figure out. How would you do it?

If th e local script that fires equipEvent is in StarterCharacter which I recommend, just play the animation there:

local character = script.Parent
local humanoid = character.Humanoid
local animator = humanoid:WaitForChild("Animator")

local equipEvent = game:GetService("ReplicatedStorage").equipEvent

local playAnimation = animator:LoadAnimation(script:FindFirstChild("Animation"))

function onEquip()
  equipEvent:FireServer()
  playAnimation:Play()
end

function stopAnimation()
  playAnimation:Stop()
end

Is this better?

-- client

local ps = game:GetService("Players")
local uis = game:GetService("UserInputService")
local rs = game:GetService("ReplicatedStorage")

local events = rs.events
local equipEvent = events:WaitForChild("equipEvent")

local plr = ps.LocalPlayer
local char = plr.Character
local weapon = char:WaitForChild("Weapon")

local mainAnims = weapon.mainAnims
local animator = char.Humanoid:WaitForChild("Animator")

local equipAnim = animator:LoadAnimation(mainAnims.Equip)
local unequipAnim = animator:LoadAnimation(mainAnims.Unequip)
local holdAnim = animator:LoadAnimation(mainAnims.Hold)

local equip = false

uis.InputBegan:Connect(function(input, isTyping)
	if isTyping then return end
	
	if input.KeyCode == Enum.KeyCode.One then
		
		
		if not equip then
			equip = true
			
			equipAnim:Play()
			equipEvent:FireServer()
			
			equipAnim:GetMarkerReachedSignal("HoldPlay"):Connect(function()
				holdAnim:Play()
			end)
		else
			equip = false
			
			holdAnim:Stop()
			
			unequipAnim:Play()
			equipEvent:FireServer()
		end
	end
end)
-- weldServer

local rs = game:GetService("ReplicatedStorage")
local ps = game:GetService("Players")

local events = rs.events
local equipEvent = events:WaitForChild("equipEvent")

local models = rs.models
local weapons = models.weapons

local weaponsWeld = script.WeaponsWeld

ps.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local torso = char.Torso		
		local charValues = char:WaitForChild("charValues")
		
		if charValues then
			charValues.Equipped.Value = false
			local weapon = weapons[charValues.Weapon.Value]:Clone()
			
			weapon.Parent = char
			weapon.Name = "Weapon"
			
			local weld = Instance.new("Motor6D")
			weld.Parent = char
			weld.Name = "weaponWeld"
			
			weld.Part0 = torso
			weld.Part1 = weapon
			
			weld.C1 = weaponsWeld[charValues.Weapon.Value].weaponRest.C1
		end
	end)
end)

equipEvent.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local charValues = char.charValues
	local weld = char.weaponWeld
	
	local torso = char.Torso
	local rightArm = char["Right Arm"]
	local weapon = char.Weapon
	
	local length1 = weapon.mainAnims.Equip.weldTime
	local length2 = weapon.mainAnims.Unequip.weldTime
	
	if not charValues.Equipped.Value  then
		charValues.Equipped.Value = true
		
		task.wait(length1.Value)
		weld.Part0 = rightArm
		weld.C1 = weaponsWeld[charValues.Weapon.Value].weaponHold.C1
		weld.CurrentAngle = 0
	else
		charValues.Equipped.Value = false
		
		task.wait(length2.Value)
		weld.Part0 = torso
		weld.C1 = weaponsWeld[charValues.Weapon.Value].weaponRest.C1
		weld.CurrentAngle = 0
	end
end)

wait I made an error
Lemme edit

Looks good! Just make sure it works well.