Problem with right arm follow mouse

So I got the problem is the ‘Right Arm’ doesn’t move normally.
GIF:
https://gyazo.com/c5c80ded217ce3ccf88a38e719e80061

Here the code I use:

local Tool = script.Parent
while not Tool:FindFirstChild('Handle') do Tool.ChildAdded:wait() end
local Handle = Tool.Handle
local Equipped = false
local Player;
local RightShoulder;
local RightArm;
local RightWeld;
local Torso;
local Mouse;

local function Slerp(t, a, b)
	local om = math.acos(a:Dot(b))
	return (math.sin((1-t)*om)/math.sin(om))*a + 
	       (math.sin(t*om)/math.sin(om))*b
end

local function GetFireCFrame()
	local from = (Torso.CFrame*CFrame.new(1.5, 0.5, 0)).p
	local fireDir = (Mouse.Hit.p-from).unit
	local torsoDir = Torso.CFrame.lookVector
	--
	local theta = math.acos(torsoDir:Dot(fireDir))
	if theta > math.pi/2 then
		fireDir = Slerp((math.pi/2)/theta, torsoDir, fireDir)
	end
	return CFrame.new(from, from+fireDir)
end

Tool.Equipped:connect(function(mouse)
	Mouse = mouse
	Equipped = true
	--
	Player = game.Players:GetPlayerFromCharacter(Tool.Parent)
	Torso = Player.Character:FindFirstChild('Torso')
	if not Torso then return end
	RightArm = Player.Character:FindFirstChild('Right Arm')
	if not RightArm then return end
	RightShoulder = Torso:FindFirstChild('Right Shoulder')
	if not RightShoulder then return end
	--
	wait(0.5)
	--
	local LastFireTime = 0
	RightShoulder.Part1 = nil
	RightWeld = Instance.new('Motor', Torso)
	RightWeld.Part0 = Torso
	RightWeld.Part1 = RightArm
	--
	while Equipped do
		local timeSinceLastFire = (tick()-LastFireTime)
		local DesiredArmCF;
		if timeSinceLastFire > 1.4 then
			local fire = GetFireCFrame()
			DesiredArmCF = fire*CFrame.Angles(math.pi/2,0,0)*CFrame.new(0,-0.5,0)
		elseif timeSinceLastFire > 1.0 then
			local frac = (timeSinceLastFire-1.0)/0.4
			local from = (Torso.CFrame*CFrame.new(1.5, 0.5, 0)).p
			local to = (Torso.CFrame*
			            CFrame.new(1.5, 0.5, 0)*
			            CFrame.Angles(0, math.pi/2*frac, 0)*
			            CFrame.new(1.5, 0, 0)).p
			local dirCur = (to-from).unit
			local dirWant = GetFireCFrame().lookVector
			local dir = Slerp(frac, dirCur, dirWant)
			DesiredArmCF = CFrame.new(from, from+dir)*CFrame.Angles(math.pi/2,0,0)*CFrame.new(0,-0.5,0)				
		else
			local frac = timeSinceLastFire/1.0
			local from = (Torso.CFrame*CFrame.new(1.5*frac, 0.5, 0)).p
			local to = (Torso.CFrame*CFrame.new(3, 0, 0)).p
			DesiredArmCF = CFrame.new(from, to)*CFrame.Angles(math.pi/2,0,0)*CFrame.new(0,-0.5,0)			
		end
		local newC1 = DesiredArmCF:inverse()*Torso.CFrame
		RightWeld.C0 = CFrame.new()
		RightWeld.C1 = newC1
		--
		wait()
	end
end)

Tool.Unequipped:connect(function(mouse)
	Equipped = false
	--
	if RightShoulder then
		RightWeld:Destroy()
		RightWeld = nil
		RightShoulder.Part1 = RightArm
		RightShoulder = nil
	end
end)
1 Like

Does the problem occur after unequipping the tool and requiring it, because I think it has something to do with the welds.

No, it happen when I equipped the tool.

I think it has something to do with the weld or math you are using, that all I can help you with right now because I am currently on mobile.