How Could i fix this bug on my lock on system?

Hey, Ive got this bug where the player is kinda moving weird instead of normally.
How do i fix this?

External Media
function FindNearestTarg(HRP)
	local TargetDist = 50
	local Target

	for i, v in pairs(workspace:GetDescendants()) do
		if v:IsA("Model") and v:FindFirstChild("HumanoidRootPart") and v.Name ~= Char.Name then
			local TargetHRP = v.HumanoidRootPart
			local Mag = (HRP.Position - TargetHRP.Position).Magnitude
			if Mag < TargetDist then
				TargetDist = Mag
				Target = TargetHRP
			end
		end
	end
	return Target
end
tool.Equipped:Connect(function()
	IdleAnim:Play()
	
	UIS.InputBegan:Connect(function(Key, IsTyping)
		if IsTyping then return end

		if Key.UserInputType == Enum.UserInputType.MouseButton1 then
			if LockOn then
				LockOn = false
				WritingAnim:Stop()
				Target = nil
				Hum.AutoRotate = true
				M2("On")
				UIS.MouseIconEnabled = true
			else
				LockOn = true
				WritingAnim:Play()
				Target = FindNearestTarg(HRP)
				Hum.AutoRotate = false
				M2("Off")
				UIS.MouseIconEnabled = false
			end
		end
	end)
end)

RunServ.RenderStepped:Connect(function()
	if Target and LockOn then
		local Mag = (HRP.Position - Target.Position).Magnitude
		if Mag < 50 then
			UIS.MouseBehavior = Enum.MouseBehavior.Default
			local targetCFrame = CFrame.lookAt(Cam.CFrame.Position, Target.Position) * CFrame.new(3,1,0)
			local lerpedCFrame = Cam.CFrame:lerp(targetCFrame, 0.2) -- make the value lower for a slower transition.
			Cam.CFrame = lerpedCFrame
			--Cam.CFrame = CFrame.lookAt(Cam.CFrame.Position, Target.Position) * CFrame.new(3,1,0)
			local Direction = (HRP.Position - Target.Position).Unit * Vector3.new(-1,0,-1)
			HRP.CFrame = CFrame.lookAt(HRP.Position,HRP.Position * Direction)
		else
			Target = nil
			LockOn = false
			Hum.AutoRotate = false
			M2("On")
			UIS.MouseIconEnabled = true
		end
	end
end)

fixed I did * Direction instead of + Direction.

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