Animated tool not stopping correctly

So…

Im creating a animated tool with a inspection animation when you press a key or double-click on the screen if the client is on mobile.

Problem:

If the ‘Inspect Animation’ is playing while the player unequips the tool, the tool doesn’t reset the animation properly and stays in the last position it was in before being unequipped.

Video of the problem:

Maybe you have some questions:

  1. Does the tool have a handle or not? - No, the tool doesn’t have a handle.

  2. What did you use for detection for the tool in the character? - I used “ChildAdded” and “ChildRemoved”.

  3. Do you connect the tool and the arm with a Motor6D? - Yes.

  4. Why did you used a The Mimic lantern? - Because that’s a base for the lantern (Basically, a lantern testing) (Model taken from the toolbox).

More details:

I tried to detect the Tool in the character with “ChildAdded” and verify if the Child Is a Tool. If is a tool, the Custom tool animation starts. Now, that Child actives the “Inspect System” (Basically, a system that play a animation with the tool with a “New Grip” (Motor6D)). So, when the player unequipped the tool while the Inspect Animation is playing, idk why but the Motors6D of the Lantern (or tool) doesn’t reset and stays in the last position it was in before being unequipped.

All Scripts:

Script (Server):

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LanternRemotes = ReplicatedStorage:FindFirstChild("Lantern_Remotes")

Players.PlayerAdded:Connect(function(_Player)
	LanternRemotes:FindFirstChild("ConnectTool").OnServerEvent:Connect(function(Player, BodyAttach, Status)
		if Player ~= _Player then return end
		if not Player.Character then return end

		local _Character = Player.Character
		local _Humanoid = _Character:FindFirstChildOfClass("Humanoid")
		if not _Humanoid then return end

		local LimbName = _Humanoid.RigType == Enum.HumanoidRigType.R6 and "Right Arm" or "RightHand"
		local Limb = _Character:FindFirstChild(LimbName)
		if not Limb then return end

		if Status == "Connect" then
			if not Limb:FindFirstChild("NewRightGrip_Server") then
				local NewMotor6D = Instance.new("Motor6D")
				NewMotor6D.Name = "NewRightGrip_Server"
				NewMotor6D.Part0 = Limb
				NewMotor6D.Part1 = BodyAttach
				NewMotor6D.C0 = CFrame.new(0, -1, 0) * CFrame.Angles(math.rad(-90), 0, 0)
				NewMotor6D.C1 = CFrame.new(0, 0, 0)
				NewMotor6D.Parent = Limb
			end
		elseif Status == "Disconnect" then
			local Grip = Limb:FindFirstChild("NewRightGrip_Server")
			if Grip then
				Grip:Destroy()
			end
		end
	end)
end)

LocalScript (Client):

local _Player: Player = game:GetService("Players").LocalPlayer
local _UserInputService = game:GetService("UserInputService")
local _LanternRemotes = game:GetService("ReplicatedStorage"):FindFirstChild("Lantern_Remotes")
local _Character: Model = _Player.Character or _Player.CharacterAdded:Wait()
local _Humanoid: Humanoid = _Character:FindFirstChildOfClass("Humanoid")
local _Animator = _Humanoid:FindFirstChildOfClass("Animator")

local LimbName = _Humanoid.RigType == Enum.HumanoidRigType.R6 and "Right Arm" or "RightHand"
local Limb = _Character:WaitForChild(LimbName)

local Animations = {
	Default = Instance.new("Animation"),
	Inspect = Instance.new("Animation")
}

local AnimationSettings = {
	CurrentAnimations = {
		Default = nil,
		Inspect = nil
	},
	CanInspect = true
}

local function ConnectMotor6D(Part1: BasePart)
	if Limb:FindFirstChild("NewRightGrip_Server") then
		warn("Server Motor6D exists.")
		return
	end
	local NewMotor6D = Instance.new("Motor6D")
	NewMotor6D.Name = "NewRightGrip_Client"
	NewMotor6D.Parent = Limb
	NewMotor6D.Part0 = Limb
	NewMotor6D.Part1 = Part1
	NewMotor6D.C0 = CFrame.new(0, -1, 0) * CFrame.Angles(math.rad(-90), 0, 0)
	NewMotor6D.C1 = CFrame.new(0, 0, 0)
	_LanternRemotes.ConnectTool:FireServer(Part1, "Connect")

	while not Limb:FindFirstChild("NewRightGrip_Server") do
		task.wait()
	end
	if Limb:FindFirstChild("NewRightGrip_Client") then
		Limb.NewRightGrip_Client:Destroy()
	end
end

local function TryPlayInspect()
	if not AnimationSettings.CanInspect then return end

	local Tool = _Character:FindFirstChildOfClass("Tool")
	if not Tool then return end

	local ToolSettingsModule = Tool:FindFirstChild("ToolSettings")
	if not ToolSettingsModule then return end

	local ToolSettings = require(ToolSettingsModule)
	if not ToolSettings.InspectAnim then return end

	AnimationSettings.CanInspect = false
	Animations.Inspect.AnimationId = "rbxassetid://".. ToolSettings.InspectAnim
	AnimationSettings.CurrentAnimations.Inspect = _Animator:LoadAnimation(Animations.Inspect)
	AnimationSettings.CurrentAnimations.Inspect.Priority = Enum.AnimationPriority.Action3
	AnimationSettings.CurrentAnimations.Inspect:Play()

	task.delay(AnimationSettings.CurrentAnimations.Inspect.Length, function()
		AnimationSettings.CanInspect = true
	end)
end

if _UserInputService.TouchEnabled and not _UserInputService.KeyboardEnabled then
	local LastTap = 0
	_UserInputService.TouchTap:Connect(function()
		local Now = tick()
		if Now - LastTap < 0.4 then
			TryPlayInspect()
		end
		LastTap = Now
	end)
else
	_UserInputService.InputBegan:Connect(function(Input, Typing)
		if Typing then return end
		if Input.KeyCode == Enum.KeyCode.U then
			TryPlayInspect()
		end
	end)
end

_Character.ChildAdded:Connect(function(ChildAdded)
	if ChildAdded:IsA("Tool") then
		local BodyAttach = ChildAdded:FindFirstChild("BodyAttach")
		if BodyAttach then
			ConnectMotor6D(BodyAttach)
		end
		local ToolSettings = require(ChildAdded:FindFirstChild("ToolSettings"))
		if ToolSettings and ToolSettings.DefaultAnim then
			Animations.Default.AnimationId = "rbxassetid://".. ToolSettings.DefaultAnim
			AnimationSettings.CurrentAnimations.Default = _Animator:LoadAnimation(Animations.Default)
			AnimationSettings.CurrentAnimations.Default:Play()
			AnimationSettings.CurrentAnimations.Default.Priority = Enum.AnimationPriority.Action
		else
			print("Invalid ToolSetting or Invalid DefaultAnim")
		end
	end
end)

_Character.ChildRemoved:Connect(function(ChildRemoved)
	if ChildRemoved:IsA("Tool") then
		if AnimationSettings.CurrentAnimations.Default then
			AnimationSettings.CurrentAnimations.Default:Stop()
			AnimationSettings.CurrentAnimations.Default = nil
		end
		if AnimationSettings.CurrentAnimations.Inspect then
			local track = AnimationSettings.CurrentAnimations.Inspect
			track:Stop()
			task.wait()
			AnimationSettings.CurrentAnimations.Inspect = nil
			AnimationSettings.CanInspect = true
			local ResetAnim = Instance.new("Animation")
			ResetAnim.AnimationId = "rbxassetid://0"
			local ResetTrack = _Animator:LoadAnimation(ResetAnim)
			ResetTrack.Priority = Enum.AnimationPriority.Action3
			ResetTrack:Play()
			ResetTrack:Stop()
		end
		for _, track in ipairs(_Animator:GetPlayingAnimationTracks()) do
			print("Active Tracks:", track.Animation.AnimationId)
		end
		if Limb:FindFirstChild("NewRightGrip_Client") then
			Limb.NewRightGrip_Client:Destroy()
		end
		_LanternRemotes.ConnectTool:FireServer(ChildRemoved:FindFirstChild("BodyAttach"), "Disconnect")
	end
end)

_Player.CharacterAdded:Connect(function(_Character)
	_Character = _Player.Character
	_Humanoid = _Character:FindFirstChildOfClass("Humanoid")
	_Animator = _Humanoid:FindFirstChildOfClass("Animator")
end)

Conclusion

Pls help me with this system. Did I do something wrong? idk lol :man_shrugging::pray:

Too much information. You can take a breath now!

Why not reset it’s position whenever un-equipped?

I’m trying to do what you just said and it’s still not working… although I did check that the tool, when going to the backpack, doesn’t reset the motor6d or the animation in the tool.