Animation doesn't play on tool correctly that i want

So i’m trying to make a pre- and unsheathe animation.

On equip on weapon, the player places his hand ready to unsheathe the weapon out.
Pressing E will play the animation, and nothing else further for now.
The problem is that the animation does play correctly but the sword being animated doesn’t follow correctly.

Did some research and tried using Motor6D instead of weld, but that didn’t do anything.

The problem:
robloxapp-20220611-1511332.wmv (1.4 MB)

What I want to achieve:
robloxapp-20220611-1515490.wmv (685.1 KB)

The code (script, parent is the tool: katana) :

local Players = game:GetService("Players")
local LocalPlayer = Players


local KatanaTool = script.Parent
local wholeKatana = KatanaTool:GetChildren()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local PreUnSheatheAnimID = "rbxassetid://9876805402"
local Animation = Instance.new("Animation")
local AnimTrack

local isEquipped = false
local isPreUnSheathe = false
local isSheathe = false
local dupModel


local function PlayerPreUnSheathe()
	if isEquipped and not isPreUnSheathe and not isSheathe then
		local Character = script.Parent.Parent
		dupModel = ReplicatedStorage.Combat.Katana1.Handle:Clone()
		dupModel.Transparency = 1
		local motor6d = Instance.new("Motor6D", Character.Torso)
		motor6d.Part0 = Character.Torso
		motor6d.Part1 = dupModel
		motor6d.C1 = CFrame.new(0.817922592, -0.80175662, 1.08306026, -0.0594392158, -0.25851348, 0.964186966, 0.0052002687, -0.965962172, -0.25866881, 0.998218358, -0.0103609078, 0.058759626)
		dupModel.Parent = Character
		local CharHumanoid = Character:WaitForChild("Humanoid")
		local CharacterAnimator = CharHumanoid.Animator
		Animation.AnimationId = "rbxassetid://9876805402"
		AnimTrack = CharacterAnimator:LoadAnimation(Animation)
		AnimTrack.Looped = true
		AnimTrack:Play()
		isPreUnSheathe = true
		repeat
			wait()
		until UIS.InputBegan:Connect(function(Input, GPE)
			if GPE then return end
			if Input.KeyCode == Enum.KeyCode.E and isEquipped and not isSheathe then
				AnimTrack:Stop() --stops preUnsheathe
				Animation:Destroy() --destroys animation
				local Animation = Instance.new("Animation", script)
				Animation.AnimationId = "rbxassetid://9876526167"
				AnimTrack = CharacterAnimator:LoadAnimation(Animation)
				AnimTrack.Priority = Enum.AnimationPriority.Action
				AnimTrack.Looped = false
				AnimTrack:Play()
				wait(.1)
				dupModel.Transparency = 0
				isSheathe = true

			end
		end)
	end
end

KatanaTool.Equipped:Connect(function()
	KatanaTool.Handle.Transparency = 1
	KatanaTool.Handle.Part.Transparency = 1
	KatanaTool.Handle.Sword.Transparency = 1
	isEquipped = true
	local Character = script.Parent.Parent
	local CharHumanoid = Character:WaitForChild("Humanoid")
	local CharacterAnimator = CharHumanoid.Animator
	PlayerPreUnSheathe()

end)


KatanaTool.Unequipped:Connect(function()
	isEquipped = false
	isPreUnSheathe = false
	isSheathe = false
	Animation:Destroy()
	dupModel:Destroy()
end)

also i’m sorry if i give you a headache :frowning:

Alright so i pretty much decided to keep going and had many problems like…

  1. 2 animations playing at same time
  2. AnimationItem disappearing and reappearing multiplied by 2? :thinking:
  3. forgetting to drink water

what have i fixed?
basically all of them

I’m finally able to finish the Localscript alone for the tool and learned quite a lot of scripting in the animation category.
take a look at the script if anyone has the same problem like me.

also sorry again if reading it gives you a headache :frowning:

local Players = game:GetService("Players")

local KatanaTool = script.Parent
local UIS = game:GetService("UserInputService")



local isEquipped = false
local isPreUnSheathe = false
local isSheathe = false

local PreUnSheatheAnim = script:WaitForChild("PreSheathe")
local UnSheatheAnim = script:WaitForChild("UnSheathe")

local function ClearAnimationItems()
	local animationsUnderScript = script:GetChildren()
	for i,v in pairs(animationsUnderScript) do
		if v.Name == "Animation" then
			--v:Destroy()
			print(v)
		end
	end
end

local function PlayerPreUnSheathe()
	if isEquipped and not isPreUnSheathe and not isSheathe then
		--ClearAnimationItems()
		local Character = script.Parent.Parent
		local CharHumanoid = Character:WaitForChild("Humanoid")
		local CharacterAnimator = CharHumanoid.Animator
		AnimTrack = CharacterAnimator:LoadAnimation(PreUnSheatheAnim)
		AnimTrack.Looped = true
		AnimTrack:Play()
		print("preunsheathe")
		isPreUnSheathe = true
		isSheathe = false
		UIS.InputBegan:Connect(function(Input, GPE)
			if GPE then return end
			if Input.KeyCode == Enum.KeyCode.E and isEquipped and isPreUnSheathe and not isSheathe then
				--ClearAnimationItems()--destroys animation
				AnimTrack:Stop() --stops preUnsheathe, now doing sheathe, equiping
				AnimTrack = CharacterAnimator:LoadAnimation(UnSheatheAnim)
				AnimTrack.Priority = Enum.AnimationPriority.Action
				AnimTrack.Looped = false
				AnimTrack:Play()
				AnimTrack.Stopped:wait()
				wait(.1)
				print("unsheathe")
				KatanaTool.Handle.Transparency = 0
				KatanaTool.Handle.Part.Transparency = 0
				KatanaTool.Handle.Sword.Transparency = 0
				isSheathe = true
				isPreUnSheathe = false
			elseif Input.KeyCode == Enum.KeyCode.E and isEquipped and not isPreUnSheathe and isSheathe then
				--unequip since already isSheathe

				AnimTrack = CharacterAnimator:LoadAnimation(UnSheatheAnim)
				AnimTrack.Priority = Enum.AnimationPriority.Action
				AnimTrack.Looped = false
				AnimTrack:Play(0.15,5,-1)
				AnimTrack.Stopped:wait()
				print("unequiped")
				isEquipped = false
				isPreUnSheathe = false
				isSheathe = false
				Character.Humanoid:UnequipTools()
			elseif Input.KeyCode == Enum.KeyCode.E and isEquipped and isPreUnSheathe and isSheathe then

				AnimTrack = CharacterAnimator:LoadAnimation(UnSheatheAnim)
				AnimTrack.Priority = Enum.AnimationPriority.Action
				AnimTrack.Looped = false
				AnimTrack:Play(0.15,5,-1)
				AnimTrack.Stopped:wait()
				print("unequiped")
				isEquipped = false
				isPreUnSheathe = false
				isSheathe = false
				Character.Humanoid:UnequipTools()			
			elseif not isEquipped then
				isEquipped = false
				isPreUnSheathe = false
				isSheathe = false
				--ClearAnimationItems()
				AnimTrack:Stop()
			end
		end)
	elseif not isEquipped then
		isEquipped = false
		isPreUnSheathe = false
		isSheathe = false
		--ClearAnimationItems()	
		AnimTrack:Stop()
	end
end



KatanaTool.Equipped:Connect(function()
	local Character = script.Parent.Parent
	local CharHumanoid = Character:WaitForChild("Humanoid")
	local CharacterAnimator = CharHumanoid.Animator
	--ClearAnimationItems()
	local a
	a = CharacterAnimator.AnimationPlayed:Connect(function(AnimTrack)
		AnimTrack:Stop()
	end)
	a:Disconnect()
	KatanaTool.Handle.Transparency = 1
	KatanaTool.Handle.Part.Transparency = 1
	KatanaTool.Handle.Sword.Transparency = 1
	isEquipped = true
	isPreUnSheathe = false
	isSheathe = false
	PlayerPreUnSheathe()
end)


KatanaTool.Unequipped:Connect(function()
	--ClearAnimationItems()	
	AnimTrack:Stop()
	isEquipped = false
	isPreUnSheathe = false
	isSheathe = false
	print("player unequiped")
end)



LocalScript location: under tool
Tool location: in starterpack

The results:
robloxapp-20220612-0052580.wmv (1.6 MB)

What about a serverscript?
i don’t know how to write one for that but if anyone wants to help out I’d be grateful. :smiley:
what a long day.

edit: script cleaned up, just decided to preload animation
also why did it print 11 times? i feel like performance would be bad if the function get’s called that many times. anyone know how to fix this?
image