Second Animation not Stopping

Hi! I’m making it so animations will play when a dummy is clicked, and then stopped when it’s clicked again. This works fine if you click only 1 dummy but if you click anymore then that it bugs out and will not stop the animation but will reset the neck’s CFrame. Any reason why this is happening?? Video footage below.

Here’s my scripts.

--Click Script
plr = game.Players.LocalPlayer
Mouse = plr:GetMouse()
local function OnLeftClick(plr)

	local Target = Mouse.Target
	if Target ~= nil then	
		if Target.Parent:FindFirstChild("HumanoidRootPart") ~= nil then
			local ClickedCharacter = Target.Parent

			print("Player clicked on "..ClickedCharacter.Name)
			local chosen = Target.Parent.Values:WaitForChild("Chosen")
			print(chosen.Value)
			if chosen.Value == true then
				chosen.Value = false
			elseif chosen.Value == false then
				chosen.Value = true
			end
			
			game.ReplicatedStorage.Events.Clicked:FireServer(ClickedCharacter.Name, Target.Parent.Values.Type.Value, Target.Parent.Values.Chosen.Value)
			if ClickedCharacter.Parent == game.Workspace.Clones then
				game.ReplicatedStorage.Events.Correct:FireServer(ClickedCharacter.Name)
			else
			
			end
		end
	end
end

Mouse.Button1Down:Connect(OnLeftClick) -- Connect OnLeftClick() to an event

-- LocalScript
clickedev.OnClientEvent:Connect(function(clicked, typeis, chosen)
	local count = 0
	if chosen == true then
		count = count + 1
		local char = game.Workspace[typeis.."s"]:WaitForChild(clicked)

		local function worldCFrameRotationToC0ObjectSpace(motor6DJoint,worldCFrame)
			local part1CF = motor6DJoint.Part1.CFrame
			local c1Store = motor6DJoint.C1
			local c0Store = motor6DJoint.C0
			local relativeToPart1 =c0Store*c1Store:Inverse()*part1CF:Inverse()*worldCFrame*c1Store

			local goalC0CFrame = relativeToPart1.Rotation+c0Store.Position--New orientation but keep old C0 joint position
			return goalC0CFrame
		end

		local goalWorldSpaceRotationCFrame = CFrame.new(char.Head.Position, game.Workspace.CameraPos.Position)
		local headMotor = char.Torso.Neck
		headMotor.C0 = worldCFrameRotationToC0ObjectSpace(headMotor, goalWorldSpaceRotationCFrame)

		animator = char:WaitForChild("Humanoid"):LoadAnimation(char.Wave)
		animator.Looped = true
		animator:Play()

	elseif chosen == false then
		animator:Stop()
		animator.Looped = false
		local char = game.Workspace[typeis.."s"]:WaitForChild(clicked)
		local headMotor = char.Torso.Neck
		local oren = CFrame.fromOrientation(math.rad(-90), math.rad(-180), 0)
		
		headMotor.C0 = CFrame.new(0,1,0) * oren
	end
	

end)

-- This script exists just to pass the event to the client.
local events = game.ReplicatedStorage.Events

local correct = events.Correct
local clickedev = events.Clicked

clickedev.OnServerEvent:Connect(function(plr, clicked, typeis, chosen)
	clickedev:FireClient(plr, clicked, typeis, chosen)
end)


correct.OnServerEvent:Connect(function(plr, clicked)
	correct:FireClient(plr, clicked)
end)

Fixed it myself!! I put

	for _, AnimationTrack in pairs(char:WaitForChild("Humanoid"):GetPlayingAnimationTracks()) do
		AnimationTrack:Stop()
	end

at the beginning of the clickedev event

clickedev.OnClientEvent:Connect(function(clicked, typeis, chosen)
	local char = game.Workspace[typeis.."s"]:WaitForChild(clicked)
	for _, AnimationTrack in pairs(char:WaitForChild("Humanoid"):GetPlayingAnimationTracks()) do
		AnimationTrack:Stop()
	end
	local count = 0
	if chosen == true then
		
		count = count + 1

		local function worldCFrameRotationToC0ObjectSpace(motor6DJoint,worldCFrame)
			local part1CF = motor6DJoint.Part1.CFrame
			local c1Store = motor6DJoint.C1
			local c0Store = motor6DJoint.C0
			local relativeToPart1 =c0Store*c1Store:Inverse()*part1CF:Inverse()*worldCFrame*c1Store

			local goalC0CFrame = relativeToPart1.Rotation+c0Store.Position--New orientation but keep old C0 joint position
			return goalC0CFrame
		end

		local goalWorldSpaceRotationCFrame = CFrame.new(char.Head.Position, game.Workspace.CameraPos.Position)
		local headMotor = char.Torso.Neck
		headMotor.C0 = worldCFrameRotationToC0ObjectSpace(headMotor, goalWorldSpaceRotationCFrame)

		animator = char:WaitForChild("Humanoid"):LoadAnimation(char.Wave)
		if chosen == true then
		animator.Looped = true
		animator:Play()
		end

	elseif chosen == false then
		for _, AnimationTrack in pairs(char:WaitForChild("Humanoid"):GetPlayingAnimationTracks()) do
			AnimationTrack:Stop()
		end
		animator.Looped = false
		local headMotor = char.Torso.Neck
		local oren = CFrame.fromOrientation(math.rad(-90), math.rad(-180), 0)
		
		headMotor.C0 = CFrame.new(0,1,0) * oren
	end
	

end)

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